Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program for Number Conversion from Decimal to Binary, Decimal to Octal, Decimal to Hexa, Binary to Octal, Binary to Hexa, Octal to Hexa

Posted By: Archie Evans     Category: C Programming     Views: 27581

Write a program for Number Conversion and Create Six Functions as follows:
Decimal2Binary
Decimal2Octal
Decimal2Hexa
Binary2Octal
Binary2Hexa
Octal2Hexa

Code for Program for Number Conversion from Decimal to Binary, Decimal to Octal, Decimal to Hexa, Binary to Octal, Binary to Hexa, Octal to Hexa in C Programming

#include<stdio.h>
#include<conio.h>
#include<math.h>
int D2B(int);
int D2O(int);
int D2H(int);
int B2O(int);
int B2H(int);
int O2H(int);
int B2D(int);
int O2D(int);
main()
{
  int ch,num;
  clrscr();
  printf("\n1.Decimal 2 Binary.");
  printf("\n2.Decimal 2 Octal.");
  printf("\n3.Decimal 2 Hexa.");
  printf("\n4.Binary 2 Octal.");
  printf("\n5.Binary 2 Hexa.");
  printf("\n6.Octal 2 Hexa.");
  printf("\n7.Exit.");
  printf("\nEnter your Choice:");
  scanf("%d",&ch);
  switch(ch)
   {
     case 1:
          clrscr();
          printf("\nEnter any Decimal number:");
          if(scanf("%d",&num)!=0 && num>0)
        D2B(num);  //calling functionelse
           printf("\nNot Allowed!!");
          break;
     case 2:
          clrscr();
          printf("\nEnter any Decimal number:");
          if(scanf("%d",&num)!=0 && num>0)
        D2O(num);  //calling functionelse
           printf("\nNot Allowed!!");
          break;
     case 3:
          clrscr();
          printf("\nEnter any Decimal number:");
          if(scanf("%d",&num)!=0 && num>0)
        D2H(num);  //calling functionelse
           printf("\nNot Allowed!!");
          break;
     case 4:
          clrscr();
          printf("\nEnter any binary number:");
          if(scanf("%d",&num)!=0)// && (num==0 || num==1))
        B2O(B2D(num));  //calling functionelse
        printf("\nNot Allowed!!");
          break;
     case 5:
          clrscr();
          printf("\nEnter any binary number:");
          if(scanf("%d",&num)!=0)// && (num==0 || num==1))
        B2H(B2D(num));  //calling functionelse
        printf("\nNot Allowed!!");
          break;
     case 6:
          clrscr();
          printf("\nEnter any Octal number:");
          if(scanf("%d",&num)!=0)
        D2H(O2D(num));  //calling functionelse
           printf("\nNot Allowed!!");
          break;
     case 7:
          exit(0);
     default:
         printf("\nEnter proper Choice:");
         break;

   }
 getch();
}

int D2B(int dec) //decimal to binary
{
  int bin=0,*bin_arr,count=0;
  while(dec>0)
   {
     bin=dec%2;
     dec/=2;
     *(bin_arr+count)=bin;
     count++;
   }
  printf("\nBINARY=");
  while(count > 0)
   {
    --count;
    printf("%d",*(bin_arr+count));
   }
   return 0;
}

int D2O(int dec) //decimal to octal
{
  int oct=0,*oct_arr,count=0;
  while(dec>0)
   {
     oct=dec%8;
     dec/=8;
     *(oct_arr+count)=oct;
     count++;
   }
  printf("\nOCTAL=");
  while(count > 0)
   {
    --count;
    printf("%d",*(oct_arr+count));
   }
   return 0;
}

int D2H(int dec) //decimal to hexa
{
  int hexa=0,*hexa_arr,count=0;
  while(dec>0)
   {
     hexa=dec%16;
     dec/=16;
     *(hexa_arr+count)=hexa;
     count++;
   }
  printf("\nHEXA=");
  while(count > 0)
   {
    --count;
     switch(*(hexa_arr+count))
     {
       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;
       default:
            printf("%d",*(hexa_arr+count));
            break;
     }
       }
   return 0;
}

int B2D(int bin) //binary to decimal
{
   int *bin_arr,count=0,dec=0,mod,idx=0;
   while(bin!=0)
    {
     mod=bin%10;
     bin/=10;
     *(bin_arr+count)=mod;
     count++;
    }
  while(count>0)
   {
     --count;
     bin=*(bin_arr+idx);
     dec+=bin * (int)(pow(2,idx));
     idx++;
   }
  return dec;
}

int B2O(int dec)
{
  int oct=0,*oct_arr,count=0;
  while(dec>0)
   {
     oct=dec%8;
     dec/=8;
     *(oct_arr+count)=oct;
     count++;
   }
  printf("\nOCTAL=");
  while(count > 0)
   {
    --count;
    printf("%d",*(oct_arr+count));
   }
   return 0;
}
int B2H(int dec)
{
  int hexa=0,*hexa_arr,count=0;
  while(dec>0)
   {
     hexa=dec%16;
     dec/=16;
     *(hexa_arr+count)=hexa;
     count++;
   }
  printf("\nHEXA=");
  while(count > 0)
   {
    --count;
     switch(*(hexa_arr+count))
     {
       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;
       default:
            printf("%d",*(hexa_arr+count));
            break;
     }
       }
   return 0;
}

int O2D(int oct)
{
   int *dec_arr,count=0,dec=0,mod,idx=0;
   while(oct!=0)
    {
     mod=oct%10;
     oct/=10;
     *(dec_arr+count)=mod;
     count++;
    }
  while(count>0)
   {
     --count;
     oct=*(dec_arr+idx);
     dec+=oct * (int)(pow(8,idx));
     idx++;
   }
  return dec;

}
  
Share: 



Archie Evans
Archie Evans author of Program for Number Conversion from Decimal to Binary, Decimal to Octal, Decimal to Hexa, Binary to Octal, Binary to Hexa, Octal to Hexa is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
John Hour from Malaysia Comment on: Oct 25
why I can't run the program after i enter the decimal number? The program will be terminated after I enter the number, why???

View All Comments