This is a Number Base Conversion Program written in C. It is designed to assist novice C programmers in understanding and implementing basic number base conversions of numeral system. This program allows users to perform the following conversions:
- Decimal to Binary: Converts a decimal number to its binary equivalent.
- Decimal to Octal: Converts a decimal number to its octal equivalent.
- Octal to Decimal: Converts an octal number to its decimal equivalent.
- Octal to Binary: Converts an octal number to its binary equivalent.
- Binary to Decimal: Converts a binary number to its decimal equivalent.
- Binary to Octal: Converts a binary number to its octal equivalent.
New C programmers can use this program as a reference to grasp fundamental concepts of number base conversions and modular programming in the C language.
This program utilizes several features of the C programming language to achieve its functionality. Here are some of the key C features used in the program. It also uses functions from the math.h
library for mathematical operations.
- Functions: The program uses functions to encapsulate specific conversion tasks. Functions such as
binaryToDecimal
,decimalToBinary
,octalToDecimal
,decimalToOctal
,octalToBinary
, andbinaryToOctal
are defined to perform individual conversion operations. This modular approach enhances code readability and maintainability. - Switch Statement: The program employs a
switch
statement to provide a menu-driven interface. The user can select the desired conversion option by entering a corresponding menu number. Theswitch
statement directs the program flow based on the user’s choice. - Loops (while, for): The program utilizes
while
loops in thedecimalToBinary
,decimalToOctal
,octalToBinary
, andbinaryToOctal
functions for iterative processes. Additionally, afor
loop is used in thebinaryToDecimal
andoctalToDecimal
functions to iterate through the input string. - Arrays (Strings): The program makes use of character arrays (strings) to store binary, octal, and decimal representations. Arrays like
input
,binaryOutput
,octalOutput
,binaryOutputOctal
, andoctalOutputBinary
are employed to store user input and conversion results. - Standard Input/Output: The program relies on standard input/output functions (
printf
andscanf
) for user interaction. It prompts users for input and displays conversion results through the console. - Math Library (pow function): The program uses the
pow
function from the math library to perform power calculations. This is employed in thebinaryToDecimal
andoctalToDecimal
functions to calculate the decimal equivalent of binary and octal numbers. - Conditional Statements: The program includes conditional statements (
if
) to check for specific conditions, such as determining whether a binary digit is ‘1’ in thebinaryToDecimal
function.
Number Base Conversion Program in C Language
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> // Function prototypes int binaryToDecimal(char* binary); void decimalToBinary(int decimal, char* binary); int octalToDecimal(char* octal); void decimalToOctal(int decimal, char* octal); void octalToBinary(char* octal, char* binary); void binaryToOctal(char* binary, char* octal); int main() { int choice; char input[50]; printf("Number Base Conversion Program\n"); printf("1. Binary to Decimal\n"); printf("2. Decimal to Binary\n"); printf("3. Octal to Decimal\n"); printf("4. Decimal to Octal\n"); printf("5. Octal to Binary\n"); printf("6. Binary to Octal\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter binary number: "); scanf("%s", input); printf("Decimal equivalent: %d\n", binaryToDecimal(input)); break; case 2: printf("Enter decimal number: "); int decimalInput; scanf("%d", &decimalInput); char binaryOutput[50]; decimalToBinary(decimalInput, binaryOutput); printf("Binary equivalent: %s\n", binaryOutput); break; case 3: printf("Enter octal number: "); scanf("%s", input); printf("Decimal equivalent: %d\n", octalToDecimal(input)); break; case 4: printf("Enter decimal number: "); int decimalInputOctal; scanf("%d", &decimalInputOctal); char octalOutput[50]; decimalToOctal(decimalInputOctal, octalOutput); printf("Octal equivalent: %s\n", octalOutput); break; case 5: printf("Enter octal number: "); scanf("%s", input); char binaryOutputOctal[50]; octalToBinary(input, binaryOutputOctal); printf("Binary equivalent: %s\n", binaryOutputOctal); break; case 6: printf("Enter binary number: "); scanf("%s", input); char octalOutputBinary[50]; binaryToOctal(input, octalOutputBinary); printf("Octal equivalent: %s\n", octalOutputBinary); break; default: printf("Invalid choice\n"); } return 0; } // Function to convert binary to decimal int binaryToDecimal(char* binary) { int decimal = 0; int length = strlen(binary); for (int i = length - 1, j = 0; i >= 0; i--, j++) { if (binary[i] == '1') { decimal += pow(2, j); } } return decimal; } // Function to convert decimal to binary void decimalToBinary(int decimal, char* binary) { int index = 0; while (decimal > 0) { binary[index++] = (decimal % 2) + '0'; decimal /= 2; } binary[index] = '\0'; // Reverse the binary string int start = 0; int end = index - 1; while (start < end) { // Swap characters at start and end char temp = binary[start]; binary[start] = binary[end]; binary[end] = temp; // Move towards the center start++; end--; } } // Function to convert octal to decimal int octalToDecimal(char* octal) { int decimal = 0; int length = strlen(octal); for (int i = length - 1, j = 0; i >= 0; i--, j++) { decimal += (octal[i] - '0') * pow(8, j); } return decimal; } // Function to convert decimal to octal void decimalToOctal(int decimal, char* octal) { int index = 0; while (decimal > 0) { octal[index++] = (decimal % 8) + '0'; decimal /= 8; } octal[index] = '\0'; // Reverse the octal string int start = 0; int end = index - 1; while (start < end) { // Swap characters at start and end char temp = octal[start]; octal[start] = octal[end]; octal[end] = temp; // Move towards the center start++; end--; } } // Function to convert octal to binary void octalToBinary(char* octal, char* binary) { int decimal = octalToDecimal(octal); decimalToBinary(decimal, binary); } // Function to convert binary to octal void binaryToOctal(char* binary, char* octal) { int decimal = binaryToDecimal(binary); decimalToOctal(decimal, octal); } |