convert decimal to binary / octal / hexadecimal - C-Tutorial

Latest

Sunday, 8 December 2019

convert decimal to binary / octal / hexadecimal


/* Program for convert decimal to binary / octal / hexadecimal */

#include <stdio.h>
#include <conio.h>

void main()
{
long n;
int i=0,a[20],b;

clrscr();

printf("\n Enter a decimal value: ");
scanf("%ld", &n);
printf("\n Enter base value (2, 8, 16): ");
scanf("%d", &b);

while(n!=0)
{
a[i] = n % b;
n = n/b;
i++;
}

if(b==2) {
printf("\n After conversion binary value: ");
}
else if(b==8) {
printf("\n After conversion octal value: ");
}
else {
printf("\n After conversion hexadecimal value: ");
}

for(i=i-1;i>=0;i--)
printf("%d",a[i]);

getch();
}

No comments:

Post a Comment