VOOZH about

URL: https://www.geeksforgeeks.org/c/number-system-conversion-in-c/

⇱ Number System Conversion in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number System Conversion in C

Last Updated : 23 Jul, 2025

Number system conversion is a fundamental concept in computer science and programming. It involves changing the representation of a number from one base to another, such as converting a decimal number to binary or a hexadecimal number to binary.

In this article, we will create a console program in the C language to perform various number system conversions. Our program will be capable of conversion between the following number systems:

👁 Number System Conversion in C
Number Base Conversion

Understanding Number System Conversions

Before we look into the C program, let's discuss some of the commonly used number systems:

  • Decimal (Base 10): The decimal system is the most commonly used number system, consisting of digits 0 to 9. Each digit's position represents a power of 10. For example, the decimal number 123 is represented as (1 * 102) + (2 * 101) + (3 * 100).
  • Binary (Base 2): The binary system consists of only two digits, 0 and 1. This system is mostly used in machines where each digit's position represents a power of 2. For example, the binary number 1101 is represented as (1 * 23) + (1 * 22) + (0 * 21) + (1 * 20).
  • Octal (Base 8): The octal system consists of digits 0 to 7. Each digit's position represents a power of 8. For example, the octal number 53 is represented as (5 * 81) + (3 * 80).
  • Hexadecimal (Base 16): The hexadecimal system uses digits 0 to 9 and letters A to F (or a to f) to represent values from 0 to 15. Each digit's position represents a power of 16. For example, the hexadecimal number 1A3 is represented as (1 * 162) + (10 * 161) + (3 * 160)

How the Program Works

We define functions for decimal-to-binary, binary-to-decimal, and other mentioned conversions. These functions implement the standard method for the respective conversions.

In the main function, we display a menu of conversion options and ask the user to select one. Depending on the user's choice, we take input and call the corresponding conversion function. The conversion functions perform the conversion and display the result.

C Program for Number System Conversion

Output

1. Decimal to Binary

Menu:
1. Decimal to Binary
2. Binary to Decimal
3. Decimal to Octal
4. Octal to Decimal
5. Hexadecimal to Binary
6. Binary to Hexadecimal
7. Exit
Enter your choice: 1
Enter a decimal number: 128
Decimal to Binary: 10000000

2. Binary to Decimal

Menu:
1. Decimal to Binary
2. Binary to Decimal
3. Decimal to Octal
4. Octal to Decimal
5. Hexadecimal to Binary
6. Binary to Hexadecimal
7. Exit
Enter your choice: 2
Enter a binary number: 10000000
Binary to Decimal: 128

3. Decimal to Octal

Menu:
1. Decimal to Binary
2. Binary to Decimal
3. Decimal to Octal
4. Octal to Decimal
5. Hexadecimal to Binary
6. Binary to Hexadecimal
7. Exit
Enter your choice: 3
Enter a decimal number: 100
Decimal to Octal: 144

4. Octal to Decimal

Menu:
1. Decimal to Binary
2. Binary to Decimal
3. Decimal to Octal
4. Octal to Decimal
5. Hexadecimal to Binary
6. Binary to Hexadecimal
7. Exit
Enter your choice: 4
Enter an octal number: 144
Octal to Decimal: 100

5. Hexadecimal to Binary

Menu:
1. Decimal to Binary
2. Binary to Decimal
3. Decimal to Octal
4. Octal to Decimal
5. Hexadecimal to Binary
6. Binary to Hexadecimal
7. Exit
Enter your choice: 5
Enter a hexadecimal number: a10
Hexadecimal to Binary: 101000010000

6. Binary to Hexadecimal

Menu:
1. Decimal to Binary
2. Binary to Decimal
3. Decimal to Octal
4. Octal to Decimal
5. Hexadecimal to Binary
6. Binary to Hexadecimal
7. Exit
Enter your choice: 6
Enter a binary number: 101000010000
Binary to Hexadecimal: A10

Related Articles

To learn the concept behind the conversion, you can refer to these articles:

Comment