Program to Convert Binary to Decimal number:Number System

February 6, 2025 1 Comment » Hits : 412





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 :

  1. Accept Number from User ( Follow Upper Note)
  2. Divide Number by 10 and Store Remainder in variable rem
  3. Divide Original Number by 10 .
  4. sum = sum + rem * pow(2,power); // First Iteration power=0 then Power is Incremented in each Iteration
  5. Calculate sum
  6. 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

Incoming search terms:

  • Anonymous

    Very Simple Program ,