C Program to Convert Octal Number to Decimal and Vice-Versa
Introduction
In this C programming tutorial, we will create a program to convert octal numbers to decimal and vice-versa. Octal and decimal are two different number systems. Octal is base-8, and decimal is base-10. The algorithm, C code, and explanation will cover both conversion processes.
Conversion Algorithms
Octal to Decimal Conversion
- Input Octal Number: Accept an octal number from the user.
- Initialize Decimal Result: Initialize a variable to store the decimal result.
- Convert Octal to Decimal: Starting from the rightmost digit, multiply each digit by (where is the position from the right starting with 0) and sum the results.
Decimal to Octal Conversion
- Input Decimal Number: Accept a decimal number from the user.
- Initialize Octal Result: Initialize an array to store the octal result.
- Convert Decimal to Octal: Divide the decimal number by 8 and store the remainder in the array. Repeat the process with the quotient until the quotient becomes 0. The octal equivalent is obtained by reading the array from bottom to top.
C Program for Octal to Decimal and Decimal to Octal Conversion
#include <stdio.h>
// Function to convert octal to decimal
int octalToDecimal(int octalNumber) {
int decimalNumber = 0, i = 0, remainder;
// Convert octal to decimal
while (octalNumber != 0) {
remainder = octalNumber % 10;
octalNumber /= 10;
decimalNumber += remainder * pow(8, i);
++i;
}
return decimalNumber;
}
// Function to convert decimal to octal
void decimalToOctal(int decimalNumber) {
int octalNumber[100], i = 0;
// Convert decimal to octal
while (decimalNumber != 0) {
octalNumber[i] = decimalNumber % 8;
decimalNumber /= 8;
++i;
}
// Display octal number
printf("Octal Equivalent: ");
for (int j = i - 1; j >= 0; --j) {
printf("%d", octalNumber[j]);
}
}
int main() {
int choice, number;
// Menu
printf("Choose an option:\n");
printf("1. Octal to Decimal Conversion\n");
printf("2. Decimal to Octal Conversion\n");
printf("Enter your choice (1/2): ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Octal to Decimal
printf("Enter an octal number: ");
scanf("%d", &number);
printf("Decimal Equivalent: %d\n", octalToDecimal(number));
break;
case 2:
// Decimal to Octal
printf("Enter a decimal number: ");
scanf("%d", &number);
decimalToOctal(number);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Output Example
Example 1: Octal to Decimal Conversion
Choose an option:
1. Octal to Decimal Conversion
2. Decimal to Octal Conversion
Enter your choice (1/2): 1
Enter an octal number: 345
Decimal Equivalent: 229
Example 2: Decimal to Octal Conversion
Choose an option:
1. Octal to Decimal Conversion
2. Decimal to Octal Conversion
Enter your choice (1/2): 2
Enter a decimal number: 157
Octal Equivalent: 235
Explanation
- Octal to Decimal Conversion Function: The
octalToDecimal
function converts an octal number to decimal using the positional value of digits. - Decimal to Octal Conversion Function: The
decimalToOctal
function converts a decimal number to octal by repeatedly dividing and storing remainders. - Menu: The program provides a menu for the user to choose between octal to decimal and decimal to octal conversion.
- Switch Statement: Based on the user’s choice, the program executes the corresponding conversion.
Conclusion
This C program demonstrates the conversion between octal and decimal numbers. Understanding the algorithm and the provided code can help you perform similar conversions in your programs.
Feel free to reach out if you have any questions or need further clarification!