Write a Program to convert a decimal number to Binary, octal and hexadecimal number

#include <stdio.h>

void main()
{

int dnum, choice;
printf ("Enter a decimal number ") ;
scanf("%d",&dnum) ;
printf ("1.Binary\n 2. Octal \n 3. Hexadecimal \n" );
printf ("Enter your choice ") ;
scanf("%d",&choice);
switch(choice)
{
case 1:
printf ("Binary equivalent is ");
conversion(dnum,2);
break;
case 2:
printf("Octal equivalent is ");
conversion (dnum, 8) ;
break;
case 3:
printf ("Hexadecimal equivalent is ") ;
conversion (dnum, 16);
break;
}
printf("\n");
}

void conversion(int number,int b)
{

int i=0, j ,rem;
char arr[30];
while(number>0)
{

rem=number%b;
number/=b;
if(rem>9&&rem<16)
arr[i++]=rem-10+'A' ;

else
arr[i++]=rem+'0' ;

}
for(j=i-1;j>=0;j--)
printf("%c", arr[j]);
}


find the output below:-

Leave a Reply

Your email address will not be published. Required fields are marked *