Conversion of Numbers in C
Advertisements
Number Conversion Program in C
With the help of C Language you can convert any number from decimal to binary and binary to decimal, binary to octal, binary to hexadecimal, decimal to octal and etc..
Decimal to Binary Conversion
Decimal number have base 10 and Binary number have base 2.
Decimal to Binary Conversion in C
#include<stdio.h> #include<conio.h> void main() { int no,rem[20],i=0,j; clrscr(); printf("Enter any number: "); scanf("%d",&no); while(no>0) { rem[i]=no%2; i++; no=no/2; } printf("Binary number is: "); for(j=i-1;j>=0;j--) { printf(" %d",rem[j]); } getch(); }
Output
Enter any number: 7 Binary number is: 111
Decimal to octal conversion
Decimal number have base 10 and Octal number have base 8.
Decimal to Octal Conversion Program in C
#include<stdio.h> #include<conio.h> void main() { int no,rem[20],i=0,j; clrscr(); printf("Enter any number: "); scanf("%d",&no); while(no>0) { rem[i]=no%8; i++; no=no/8; } printf("Octal number is:"); for(j=i-1; j>=0; j--) { printf(" %d",rem[j]); } getch(); }
Output
Enter any number: 23 Octal number is: 2 7
Decimal to Hexadecimal Conversion
Decimal number have base 10 and Hexadecimal number have base 16.
Decimal to Hexa Conversion Program in C
#include<stdio.h> #include<conio.h> void main() { int num,rem[20],hex=0,i=0,j; clrscr(); printf("Enter any number: "); scanf("%d",&num); while(num>0) { rem[i] = num % 16; num = num / 16; i++; } printf("Hexadecimal number: "); for(j=i-1; j>=0 ; j--) { switch(rem[j]) { 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",rem[j]); } } getch(); }
Output
Enter any number: 231 Hexadecimal number: E7
Google Advertisment