Converting numbers between systems is a skill you’ll use in both computer science and electronics. Here, we’re gonna convert an octal number, like 17, into its decimal form. Octal numbers are base-8, using digits 0-7, and decimals are base-10.
Prerequisites for the C Program to Convert Octal to Decimal
Before you start writing the program, ensure you know these:
- Understanding of loops such as
whilein C. - Usage of basic mathematical operators like
%,/, and*. - Familiarity with
<stdio.h>for input/output. - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Convert Octal to Decimal
Here's the source code for converting an octal number to its decimal equivalent. It calculates the decimal value by processing each digit.
#include <stdio.h>
int main() {
int octalNumber, decimalNumber = 0, base = 1, rem;
// User input
printf("Enter an octal number: ");
scanf("%d", &octalNumber);
// Convert octal to decimal
while (octalNumber != 0) {
rem = octalNumber % 10;
decimalNumber += rem * base;
base *= 8;
octalNumber /= 10;
}
// Output the result
printf("The decimal equivalent is: %d\n", decimalNumber);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Convert Octal to Decimal Program
When you run the program with an input, here's what you'll see:
Enter an octal number: 17
The decimal equivalent is: 15
Algorithm to Convert Octal to Decimal in C
Converting octal to decimal means evaluating each digit by its position. Here's the step-by-step algorithm this C program follows:
- Read the octal number from the user.
- Initialize
decimalNumberto 0 andbaseto 1. - Repeat while
octalNumberis not 0: - Get the last digit using
octalNumber % 10. - Update
decimalNumberby adding the product of the digit andbase. - Multiply
baseby 8. - Remove the last digit from
octalNumberby dividing by 10.
flowchart TD
A([Start]) --> B[/Read octalNumber/]
B --> C[decimalNumber = 0]
C --> D[base = 1]
D --> E{"octalNumber != 0?"}
E -->|Yes| F[rem = octalNumber %% 10]
F --> G[decimalNumber += rem * base]
G --> H[base *= 8]
H --> I[octalNumber /= 10]
I --> E
E -->|No| J([End])How the C Program to Convert Octal to Decimal Works
Let's break down how each part of the code converts octal to decimal:
#include <stdio.h>: Includes the standard input and output library for functions likeprintfandscanf.int main(): The main function where program execution begins.int octalNumber, decimalNumber = 0, base = 1, rem;: Declares variables for storing the octal number, the resulting decimal number, the base (initially set to 1), and remainder.scanf("%d", &octalNumber);: Reads an octal number input from the user and stores it inoctalNumber.while (octalNumber != 0): Loops until all digits have been processed — a fundamental control structure for this conversion.rem = octalNumber % 10;: Gets the last digit ofoctalNumber.decimalNumber += rem * base;: Converts and accumulates the current digit's decimal value.base *= 8;: Updates the base for the next digit's place value in octal.octalNumber /= 10;: Removes the processed digit fromoctalNumber.printf("The decimal equivalent is: %d\n", decimalNumber);: Outputs the calculated decimal number.return 0;: Ends the program, returning zero as the status code.
Quick Quiz — test your understanding
Tap an option to check your answerWhat base is used in an octal number system?
Time and Space Complexity of the C Program to Convert Octal to Decimal
Evaluating the efficiency of converting octal to decimal helps understand its performance.
Time Complexity: O(n), where n is the number of digits in the octal number — for instance, 2 for 17. The while (octalNumber != 0) loop processes each digit once, so 3 digits means 3 rounds.
Space Complexity: O(1). The program uses a fixed number of variables — octalNumber, decimalNumber, base, and rem. There's no extra space needed beyond these few.
Common mistake:Don’t forget to multiply base by 8 each time. It's important for weighing each digit accurately.
Key takeaway:Octal to decimal conversion’s all about assigning positional values to each digit and updating base as you go.FAQs About C Program to Convert Octal to Decimal
Here's a collection of frequently asked questions about converting octal numbers to decimal in C.
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Convert Octal to Binary with Example
- C Program to Convert Binary to Octal with Sample Output
- C Program to Convert Binary to Decimal with Code and Output
- C Program to Convert Binary to Decimal and Vice-Versa
- C Program to Convert Hexadecimal to Decimal
Conclusion
This program shows a cool way of using basic arithmetic to convert number systems. If this caught your interest, give converting binary to decimal a shot for more practice. Get the logic down, and you'll be set for more complex conversions.
Keep sharpening your C skills with quizzes on our platform. You can also check out the code in the EduSeekho online compiler anytime.