Make Your Search Easy ! :) Use me

Wednesday, August 9, 2017

Number Conversions

Program to convert decimal numbers into hexadecimal, octal and binary.

#include<iostream.h>
#include<conio.h>

//function to obtain hexadecimal value
void show_hex(int a)
{
    int decnum=a,i=0,temp;
      char hex[100];
      while(decnum>0)
      {
          temp=decnum%16;
            //to convert the obtained result into characters
            if(temp<10)
            { hex[++i]=temp+48; }
            else
            {  hex[++i]=temp+55; }
            decnum/=16;
         }
      cout<<"\n\nEquivalent Hexadecimal value for "<<a<<" is : ";
      for(i;i>0;i--)
      {
          cout<<hex[i];
         }
   }

//function to obtain octadecimal value
void show_octal(int a)
{
    int decnum=a,i=0,temp;
      char octal[100];
      while(decnum>0)
      {
          temp=decnum%8;
            //to convert the obtained result into characters
            octal[++i]=temp+48;
            decnum/=8;
         }
      cout<<"\nEquivalent Octadecimal value for "<<a<<" is : ";
      for(i;i>0;i--)
      {
          cout<<octal[i];
         }
   }

//function to obtain binary value
void show_binary(int a)
   {
    int decnum=a,i=0,temp;
      char binary[100];
      while(decnum>0)
      {
          temp=decnum%2;
            //to convert the obtained result into characters
            binary[++i]=temp+48;
            decnum/=2;
         }
      cout<<"\nEquivalent binary value for "<<a<<" is : ";
      for(i;i>0;i--)
      {
          cout<<binary[i];
         }
   }

//main program
void main()
{
    int a;
    cout<<"Enter any decimal number :";
      cin>>a;
      show_hex(a);
      show_octal(a);
      show_binary(a);
      getch();
   }

No comments:

Post a Comment