Program to Convert Binary to Decimal number:Number System
Note : As this program is for beginners (not for Experts) ,And no Validations are Provided in Program . So Please Provide Correct Inputs ( 0 and 1 ).
Logic of This Program :
- Accept Number from User ( Follow Upper Note)
- Divide Number by 10 and Store Remainder in variable rem
- Divide Original Number by 10 .
- sum = sum + rem * pow(2,power); // First Iteration power=0 then Power is Incremented in each Iteration
- Calculate sum
- Display Sum as Decimal Representation of Given Binary
Program :
#include<stdio.h> #include<conio.h> #include<math.h> void bin_dec(long int num) // Function Definition { long int rem,sum=0,power=0; while(num>0) { rem = num%10; num = num/10; sum = sum + rem * pow(2,power); power++; } printf("Decimal number : %d",sum); } //------------------------------------- void main() { long int num; clrscr(); printf("Enter the Binary number (0 and 1): "); scanf("%ld",&num); bin_dec(num); getch(); }
Output :
Enter the Binary number : 111 Decimal number : 7