Open In App

C Program For Octal to Decimal Conversion

The number system is one of the ways to represent numbers. Every number system has its own base or radix. For example, Binary, Octal, Decimal, and Hexadecimal Number systems are some of the number systems and are also used in microprocessor programming. These numbers are easy to convert from one system to another system. One can convert decimal to binary, decimal to hex, decimal to octal, and vice versa. 

 

Example:



Input: octal number = 123

Output: decimal number = 83



Using Format Specifier

Below is the C program to convert octal to a decimal using a format specifier:




// C program to demonstrate conversion of
// octal to decimal using format specifier
#include <stdio.h>
 
// Driver code
int main()
{
    int n;
    printf("Enter an Octal number: \n");
    scanf("%o", &n);
   
    // printing the result
    printf("\nDecimal Representation is: %d", n);
    return 0;
}

Output

Enter an Octal number: 123
Decimal Representation is: 83

Without using pow() function

Below is the C program to convert octal to decimal without using pow() function:




// C program to demonstrate conversion of
// octal to decimal without using pow()
#include <stdio.h>
 
// Function to convert octal to decimal
int octalToDecimal(int n)
{
    int decimalvalue = 0;
 
    // Assigning base value to 1, i.e 8^0
    int base = 1;
 
    int temp = n;
   
    // while loop executes the statements until the
    // condition is false
    while (temp)
    {
        // Finding the last digit
        int lastdigit = temp % 10;
        temp = temp / 10;
 
        // Multiplying last digit with appropriate
        // base value and adding it to decimalvalue
        decimalvalue += lastdigit * base;
 
        base = base * 8;
    }
 
    return decimalvalue;
}
 
// Driver code
int main()
{
    int octalnum = 123;
 
    printf("decimal number is %d",
           octalToDecimal(octalnum));
}

Output
decimal number is 83

Using pow() Function

Below is the C program to convert octal to decimal using the pow() function:




// C program to demonstrate conversion of
// octal to decimal using pow()
#include <math.h>
#include <stdio.h>
 
int Octaltodecimal(int octal)
{
    int decimalnumber = 0, i = 0;
 
    // while loop executes the statements
    // until the condition is false
    while (octal != 0)
    {
        // Calculating equivalent decimal number
        // for the given octal number
        decimalnumber = decimalnumber +
                        (octal % 10) * pow(8, i++);
        octal = octal / 10;
    }
    // printing the result
    return decimalnumber;
}
// Driver code
int main()
{
    int octalnumber = 123;
   
    // calling function
    printf("decimal number is %d",
           Octaltodecimal(octalnumber));
    return 0;
}

Output
decimal number is 83

Standard method(Without Using Functions)

Below is the C program to convert octal to decimals using the standard method:




// C program to demonstrate conversion of
// octal to decimal without using functions
#include <math.h>
#include <stdio.h>
 
// Driver code
int main()
{
    int octalnumber = 123, decimalnumber = 0;
    int i = 0;
   
    // while loop executes the statements
    // until the condition is false
    while (octalnumber != 0)
    {     
        // Calculating equivalent decimal number
        // for the given octal number
        decimalnumber = decimalnumber
                        + (octalnumber % 10) * pow(8, i++);
        octalnumber = octalnumber / 10;
    }
   
    // Printing the result
    printf("decimal number is: %d", decimalnumber);
    return 0;
}


Article Tags :