// CONVERSION OF BINARY TO DECIMAL USING RECURSION
/*PROCEDURE
1.Eg: Take a binary number as 1110
2.1 1 1 0
| | | |->multiplyby2^0=result0
| | |->multiply by 2^1 =result1
| |->multiply by 2^2 =result2
|->multiply by 2^3 =result 3
3.Final ,the converted decimal number=result0+result1+result2+result3
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
int todecimal(char bin[50],int len);
int len; //Using ‘len’ as global variable is optional
void main()
{
char bin[50];
int dec;
clrscr();
printf("Enter the binary number\n");
scanf("%s",bin);
len=strlen(bin);
dec=todecimal(bin,len);
printf("%d",dec);
getch();
}
int todecimal(char bin[50],int l)
{
if(len-l==len)
return 0;
else
{
if(bin[len-l]=='0')
return todecimal(bin,l-1);
else
return 1*pow(2,l-1)+todecimal(bin,l-1);
}
}
Any doubts ask it in comments.
Finally share this program,if you like this.
Thank you.

No comments:
Post a Comment