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, andbinaryToOctalare defined to perform individual conversion operations. This modular approach enhances code readability and maintainability. - Switch Statement: The program employs a
switchstatement to provide a menu-driven interface. The user can select the desired conversion option by entering a corresponding menu number. Theswitchstatement directs the program flow based on the user’s choice. - Loops (while, for): The program utilizes
whileloops in thedecimalToBinary,decimalToOctal,octalToBinary, andbinaryToOctalfunctions for iterative processes. Additionally, aforloop is used in thebinaryToDecimalandoctalToDecimalfunctions 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, andoctalOutputBinaryare employed to store user input and conversion results. - Standard Input/Output: The program relies on standard input/output functions (
printfandscanf) for user interaction. It prompts users for input and displays conversion results through the console. - Math Library (pow function): The program uses the
powfunction from the math library to perform power calculations. This is employed in thebinaryToDecimalandoctalToDecimalfunctions 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 thebinaryToDecimalfunction.
Number Base Conversion Program in C Language
#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);
}



