C program to convert Octal to Decimal number system

Write a C program to read Octal number from user and convert to Decimal number system. How to convert from Octal number system to Decimal number system in C programming. Logic to convert octal to decimal number system in C program.

Example

Input

Input octal number: 172

Output

Decimal number: 122

Required knowledge

Basic C programming, For loop

Must know - Program to find last digit of a number.

Octal number system

Octal number system is a base 8 number system. Octal number system uses 8 symbols to represent all its numbers i.e. 01234567

Decimal number system

Decimal number system is a base 10 number system. Decimal number system uses 10 symbols to represent all its numbers i.e. 0123456789

Logic to convert from octal to decimal

Octal to Decimal conversion

Algorithm Conversion Octal to Binary
begin:
    read(octal);
    decimal ← 0; rem ← 0; place ← 0;
    While(octal !=0)
    begin:
        remoctal % 10;
        decimaldecimal + (8place * rem);
        octaloctal / 10;
        placeplace + 1;
    end;
    write('Decimal =' decimal);
end;

Program to convert octal to decimal

/**
 * C program to convert Octal number system to Decimal number system
 */

#include <stdio.h>
#include <math.h>

int main()
{
    long long octal, tempOctal, decimal;
    int rem, place;
    
    /*
     * Input octal number from user
     */
    printf("Enter any octal number: ");
    scanf("%lld", &octal);
    tempOctal = octal;

    decimal = 0;
    place = 0;
    
    /*
     * Convert octal to decimal
     */
    while(tempOctal > 0)
    {
        // Extract the last digit of octal
        rem = tempOctal % 10;

        // Convert last octal digit to decimal
        decimal += pow(8, place) * rem;

        // Remove the last octal digit
        tempOctal /= 10;

        place++;
    }

    printf("Octal number = %lld\n", octal);
    printf("Decimal number = %lld", decimal);

    return 0;
}

Output

Enter any octal number: 172
Octal number = 172
Decimal number = 122

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>