## C Program To Convert Decimal into Binary, Hexadecimal and Octal

Learn How To Convert Decimal into Binary, Hexadecimal and Octal Values in C Programming. This is the simplest C Program For Converting Decimal Values into Binary, Octal and Hexadecimal Numbers.

The given C Program uses If – Else Block Structure to solve the following problems:

- Conversion of Decimal Number into Binary Value
- Converting Decimal Numbers into Hexadecimal Number
- Conversion of Decimal Integer into Octal Value

#### Decimal Number

A Decimal Number has a base 10 and includes digits the following digits: **0 1 2 3 4 5 6 7 8 9**

#### Binary Number

A Binary Value has a base 2 and includes only two digits: **0 1**

#### Hexadecimal Number

A Hexadecimal Number has a base 16 and includes the following values: **0 1 2 3 4 5 6 7 8 9 A B C D E F**

#### Octal Number

An Octal Number has a base 8 and includes the following digits: **0 1 2 3 4 5 6 7**

#### C Program To Convert Decimal into Binary, Hexadecimal and Octal Values using Functions

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include<stdio.h> #include<stdlib.h> void conversion(int num, int base) { int remainder = num % base; if(num == 0) { return; } conversion(num / base, base); if(remainder < 10) { printf("%d", remainder); } else { printf("%c", remainder - 10 + 'A' ); } } int main() { int num, choice; printf("\nEnter a Positive Decimal Number:\t"); scanf("%d", &num); while(1) { printf("\n1. Decimal To Binary Conversion"); printf("\n2. Decimal To Octal Conversion"); printf("\n3. Decimal To Hexadecimal Conversion"); printf("\n4. Quit"); printf("\nEnter Your Option:\t"); scanf("%d", &choice); switch(choice) { case 1: printf("\nBinary Value:\t"); conversion(num, 2); break; case 2: printf("\nOctal Value:\t"); conversion(num, 8); break; case 3: printf("\nHexadecimal Value:\t"); conversion(num, 16); break; case 4: exit(0); default: printf("Enter a correct input\n"); } } printf("\n"); return 0; } |

#### Output

If you have any compilation error or doubts in this C Program For Decimal Conversion into Binary, Hexadecimal and Octal Values, let us know about it in the Comment Section below.

I had never even imagined that Decimal to Binary, Hexadecimal and Octal conversion could br so easier. Thanks for providing the code.

This is really amazing. This is like 3 in 1 code for Number system conversion.

Very helpful ! Thank you.

please explain the execution

Can anyone please explain the program