Octal to Decimal in C

Octal to decimal in C: This C/C++ program converts octal number into its decimal equivalent using while loop.

What is the base of a Octal number system?
Octal system is base 8 number system. Octal numbers can be formed by using digits from 0 to 7. Decimal number system is base 10 number system. A decimal number can be formed by using digits between 0 to 9.

octal to decimal in c++

The diagram below explains how to convert octal number (67) to it's decimal equivalent value in C/C++.

Algorithm to convert octal to decimal

Octal to decimal conversion can be done using repeated division process using following algorithm:
  1. Start
  2. Accept any octal number.
  3. Use any loop to multiply each digit of the octal number by powers (starting from 0) of 8 respectively, starting from the last digit in octal number.
  4. Every time after multiplication add the result to a sum variable.
  5. Finally display the sum which is the required equivalent decimal number.
  6. Stop.
Note: While compiling the program compile like this -
cc filename.c -lm

C program to convert octal to decimal

Here is the source code of a C program to convert octal to decimal:
#include<stdio.h>
#include<math.h>

int main()
{
     int i = 0;
     long int octal_number, decimal_number = 0;

     printf("\n Enter the octal number:- ");
     scanf("%ld",&octal_number);

    while (octal_number!=0)
    {
        decimal_number = decimal_number +(octal_number % 10)* pow(8, i++);

        octal_number = octal_number / 10;
    }

    printf("\n Equivalent decimal number is %ld \n \n",decimal_number);

    return 0;

}

Output:

 Enter the octal number:- 67

 Equivalent decimal number is 55

Output of program in terminal:

Output of C program to convert octal to decimal

Program for Octal to decimal Conversion in C++

#include<iostream>
#include<math.h>
using namespace std;

//Compiler version g++ 6.3.0

int main()
{

    int i = 0;
    long int octal_number, decimal_number = 0;

    cout << "\n Enter the octal number:- ";
    cin>>octal_number;
 
    while (octal_number!=0)
    {
       decimal_number = decimal_number + (octal_number % 10)* pow(8, i++);
       octal_number = octal_number / 10;
    }

    cout<<"\n Equivalent decimal number is"<< " " <<decimal_number;

    return 0;
}

Output:

 Enter the octal number:- 67
 Equivalent decimal number is 55
C program to convert octal to binary

C program to convert octal to decimal using while loop

Here is the source code of a C program to convert octal to decimal using while loop and without math.h library:
/* C program to convert octal to decimal using while loop */

#include<stdio.h>

int main() 
{ 
    int octal_value, decimal_value = 0, base = 1, tmp, last_digit;

    printf("\n Enter any octal value:- ");
    scanf("%d",&octal_value);

    tmp = octal_value;

    while(tmp>0)
    {
       last_digit = tmp % 10; 
       tmp = tmp / 10;
       decimal_value += last_digit * base;
       base = base * 8; 
    }

    printf("\n Equivalent decimal value is:- %d",decimal_value);

}

Output:

 Enter any octal value:- 67

 Equivalent decimal value is 55

Related C programs:

  1. C program to convert octal to hexadecimal
  2. C program to convert hexadecimal to binary
  3. C program to convert binary to hexadecimal