Oct 19, 2011

C program to convert the given Binary number into its equivalent Decimal,Octal and Hexadecimal numbers.

C Program to convert binary number into its equivalent Decimal, Octal, and Hexadecimal number systems. In this program we convert the binary number to decimal number, then converted that decimal to octal and decimal to hexadecimal. Read more about C Programming Language .




/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>
#include<string.h>
void hexadecimal();
void main()
{
 int   num, bnum, dec = 0, base = 1, rem ,dec1=0,oct[25],dec2=0,flag=0,i=0,counter=0,j;


 printf("Enter the binary number(1s and 0s)\n");
 scanf("%d", &num);           

 bnum = num;

 while( num > 0)
 {
  rem = num % 10;
  if((rem==0) || (rem==1))
  {
   dec = dec + rem * base;
   num = num / 10 ;
   base = base * 2;
   flag=1;
  }
  else
  {
   flag=0;
   printf("\nEnter binary number\n");
   break;
  }
 }
 if(flag==1)
 {
  printf("The Binary number is = %d\n", bnum);
  printf("Its decimal equivalent is =%d\n", dec);
  dec1=dec;
  dec2=dec1;
  while(dec>0)
  {
   rem=dec%8;
   oct[i]=rem;
   dec=dec/8;
   i++;
   counter++;

  }
  counter--;
  printf("\n Its octal equivalent is:");
  while(counter>=0){
   printf("%d" ,oct[counter]);
   counter--;
  }

  printf("\nIts Hexa Decimal  equivalant is:  ");
  hexadecimal(dec2);

 }
}
void hexadecimal(long n)
{
 long i;
 if(n>0)
 {
  i=n%16;
  n=n/16;
  hexadecimal(n);
  if(i>=10)
  {
   switch(i)
   {
   case 10:
    printf("A");
    break;
   case 11:
    printf("B");
    break;
   case 12:
    printf("C");
    break;
   case 13:
    printf("D");
    break;
   case 14:
    printf("E");
    break;
   case 15:
    printf("F");
    break;
   }
  }
  else
   printf("%ld",i);
 }
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!