C Program to Convert Octal to Decimal


This is a C program to convert octal number to decimal.

Problem Description

This program takes a octal number as input and converts it into decimal number.

Problem Solution

1. Take a octal number as input.
2. Multiply each digits of the octal number starting from the last with the powers of 8 respectively.
3. Add all the multiplied digits.
4. The total sum gives the decimal number.

Program/Source Code

Here is source code of the C program to Convert Octal to Decimal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Convert Octal to Decimal
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  7. int main()
  8. {
  10.     long int octal, decimal = 0;
  11.     int i = 0;
  13.     printf("Enter any octal number: ");
  14.     scanf("%ld", &octal);
  15.     while (octal != 0)
  16.     {
  17.         decimal =  decimal +(octal % 10)* pow(8, i++);
  18.         octal = octal / 10;
  19.     }
  20.     printf("Equivalent decimal value: %ld",decimal);
  21.     return 0;
  22. }
Program Explanation

1. Take the octal number as input and store it in the variable octal.
2. Initialize the variables decimal and i to zero.
3. Obtain the remainder and quotient of the octal number. Multiply the remainder by powers of 8 using function pow(8, i++), add this value to the variable decimal and store it in the variable decimal.
4. Override the variable octal with quotient.
5. Repeat the steps 3 and 4 with the quotient obtained until the quotient becomes zero.
6. Print the variable decimal as output.

Runtime Test Cases
Enter any octal number: 67
Equivalent decimal value: 55

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter