Binary to Decimal in C and C++

Bismillahir Rahmanir Rahim
Solution in C:

#include<stdio.h>
#include<math.h>
void main()
{
unsigned long int binary;
 int b,decimal=0,i=0;
 printf("Enter your binary number: ");
 scanf("%ld",&binary);
 while(binary!=0)
 {
  b=binary%10;
  binary=binary/10;
  decimal=decimal+b*pow(2,i);
  i++;
 }
printf("Decimal is = %d",decimal);
}
Solution in C++:

#include<iostream.h>
#include<math.h>
void main()
{
 unsigned long int binary;
 int b,decimal=0,i=0;
 cout<<"Enter your binary number: ";
 cin>>binary;
 while(binary!=0)
 {
  b=binary%10;
  binary=binary/10;
  decimal=decimal+b*pow(2,i);
  i++;
 }
cout<<"Decimal is = "<<decimal;
}

Socializer Widget By Blogger Yard
SOCIALIZE IT →
FOLLOW US →
SHARE IT →