Friday 26 December 2014

recursion

Base conversion program from decimal


#include<stdio.h>
void convert(int dec,int base)
{
if(dec<base)
{
printf(" %d",dec);
return;
}
int n=dec%base;
convert(dec/base,base);
printf(" %d",n);
}
void main()
{
int dec,base;
printf("Enter the decimal number : ");
scanf("%d",&dec);
        printf("Enter the base : ");
        scanf("%d",&base);
        convert(dec,base);
}

         Printing Digits of a Number

function display prints digit in actual order while the function revdisplay prints the digit in reverse order
#include<stdio.h>
void display(int n)
{
if(n<10)
{
printf(" %d",n);
return;
}
int dig=n%10;
display(n/10);
    printf(" %d",dig);
}
void revdisplay(int n)
{
if(n==0)
{
return;
}
int dig=n%10;
    printf(" %d",dig);
revdisplay(n/10);
}
void main()
{
int num;
printf("Enter the number : ");
scanf("%d",&num);
printf("\nDigits of the number in actual order is : ");
display(num);
printf("\nDigits of the number in reverse order is : ");
revdisplay(num);
}

No comments:

Post a Comment