Program to convert octal to decimal in C Language

program-to-convert-octal-to-decimal-in-c-language

Program Description:

Write a Program to convert octal to decimal in C programming language. The program should accept an Octal number and converts the provided octal number to a decimal number using the while loop.

What are Octal Numbers?

Here is the definition Octal number from Wikipedia,

The octal numeral system, or oct for short, is the base-8 number system and uses the digits 0 to 7, that is to say 10octal represents eight and 100octal represents sixty-four. However, English, like most languages, uses a base-10 number system, hence a true octal system might use different vocabulary.

In the decimal system, each place is a power of ten. For example:

{\displaystyle \mathbf {74} _{10}=\mathbf {7} \times 10^{1}+\mathbf {4} \times 10^{0}}

In the octal system, each place is a power of eight. For example:

{\mathbf  {112}}_{8}={\mathbf  {1}}\times 8^{2}+{\mathbf  {1}}\times 8^{1}+{\mathbf  {2}}\times 8^{0}

Octal Number to Decimal Number conversion:

To convert an Octal number to a Decimal number. We need to get each digit of the Octal number and apply the power function.

Here is an example of an Octal number to Decimal conversion.

Convert Octal 20 to decimal number

20 = (2 × 8¹) + (0 × 8⁰) = 16

As you can see Octal 20 is equal to decimal 16

Here are the few Octal Numbers and their equivalent decimal numbers.

Octal NumberDecimal Number
00
11
22
33
44
55
66
77
108
119
1210
1311
1412
1513
1614
1715
2016

Program to Convert Octal to Decimal in C Language Algorithm:

  1. Take the input Octal number from the user. And we store the input value in variable n
  2. If the number is negative or zero, Display the error and ask for the input again ( We use the goto statement to take the program control back to the start)
  3. The main idea is to get each digit of the Octal number and use the power function to calculate the power of 8 (as it is an octal number) with respect to the digit position.
  4. The loop run until the n becomes zero or less – Condition – (n > 0). We start from the last digit and go til the first digit.
  5. At Iteration of Loop
    • We get the last digit of the number. for the first iteration, we get the last digit using the modulus operator – rem = n%10;
    • Then we calculate the rem * pow(8, i); – At the first Iteration, which is equal to pow(8, 0) and at the second iteration, It will be pow(8, 1), and so on.
    • Then add the above result to our decimal variable – decimal = decimal + rem * pow(8, i);
    • As we processed the last digit, We need to remove it from the n. To do that, We can use the Division Operation like this – n = n/10;
    • Finally, Increment the variable i. which is used in the calculation of pow
  6. The above loop continues until the n value is greater than the Zero
  7. Once the loop is terminated, The decimal variable contains the Decimal value of the provided octal number ( n). Then display the result on the console.

📢 We are going to use the pow function from the math.h header file to calculate the power. If you don’t want to use the predefined pow function, you can write a custom function as well.

Program to Convert Octal to Decimal in C Language:

Let’s convert the above algorithm into the C program.

We are going to use the pow function from the math.h library. The prototype of the pow function is

double pow(double x, double y)

Here is the program to convert the Octal number to a Decimal number in C language.

Convert Octal to Decimal in C program Output:

As we have specified above, We used the pow function to calculate the power value. And pow function is not part of the default C library, So we need to use the -lm option to include the math library functions.

So to Compile the program use the following command ( In Linux based systems)

The above command generates the executable file, which is a.out, Run the executable.

Let’s test few more examples

As you can see we are getting the expected decimal values.

What if the user enters a negative number?

If the user enters a negative number, The program displayed an error notice ( Invalid Input, Please try again) and asked for the user input again.

convert-octal-number-to-decimal-number-in-c-language

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] C Program to Convert Octal Number to Decimal Number […]

  2. […] C Program to Convert Octal Number to Decimal Number […]

  3. […] C Program to Convert Octal Number to Decimal Number […]

Leave a Reply