15. Implement Recursive functions for Binary to Decimal Conversion
Algorithm:
Step 1: start
Step 2: read number
Step 3: if number is not equal to zero , goto step 4 else goto step 7
Step 4: calculate remainder number by 2, get n value by n divide by 2
Step 5: calculate decimal by adding decimal and remainder by multiply power of 2, i
Step 6: count by increasing i by one
Step 7: print decimal
Step 8: stop
FLOWCHART:
PROGRAM:
#include <stdio.h>
#include <math.h>
int BinaryToDecimal(long long n);
int main()
{
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, BinaryToDecimal(n));
return 0;
}
int BinaryToDecimal(long long n)
{
int dec = 0, i = 0, rem;
while (n!=0)
{
rem = n % 10;
n = n / 10;
dec = dec + rem*pow(2,i);
i++;
}
return dec;
}
OUTPUT:
Algorithm:
Step 1: start
Step 2: read number
Step 3: if number is not equal to zero , goto step 4 else goto step 7
Step 4: calculate remainder number by 2, get n value by n divide by 2
Step 5: calculate decimal by adding decimal and remainder by multiply power of 2, i
Step 6: count by increasing i by one
Step 7: print decimal
Step 8: stop
FLOWCHART:
PROGRAM:
#include <stdio.h>
#include <math.h>
int BinaryToDecimal(long long n);
int main()
{
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, BinaryToDecimal(n));
return 0;
}
int BinaryToDecimal(long long n)
{
int dec = 0, i = 0, rem;
while (n!=0)
{
rem = n % 10;
n = n / 10;
dec = dec + rem*pow(2,i);
i++;
}
return dec;
}
No comments:
Post a Comment