Write a program to accept a decimal number and convert it to binary, octal and hexadecimal number.
#include<stdio.h>
#include<conio.h>
void convert(int, int);
int main()
{
int num;
//accept value from user
printf("Enter a positive decimal number : ");
scanf("%d", &num);
printf("\nBinary number : ");
convert(num, 2); //calling convert()
printf("\n");
printf("\nOctal number : ");
convert(num, 8); //calling convert()
printf("\n");
printf("\nHexadecimal number : ");
convert(num, 16); //calling convert()
printf("\n");
getch();
return 0;
}
void convert (int num, int base)
{
int rem = num%base; //rem=remainder
if(num==0)
return;
convert(num/base, base); //recursion
//printing values
if(rem < 10)
printf("%d", rem);
else
printf("%c", rem-10+'A' );
}
0 Comments