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>
  6.  
  7. int main()
  8. {
  9.  
  10.     long int octal, decimal = 0;
  11.     int i = 0;
  12.  
  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.

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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best 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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.