Program to convert Octal to Decimal

Description

To convert octal to decimal, extract rightmost digits from octal number and
multiply the digit with the proper base (Power of 8) and add it to the variable. At the
end, the variable will store the required decimal number.

C/C++

/* C Program to convert Octal to Decimal */
//Save it as ConvertOctalToDecimal.c

#include<stdio.h>
int main(){

    int octal, rem, dec = 0, count=0;

    printf("Enter a octal Number : ");
    scanf("%d", &octal);

    //Storing the value of binary to temporary variable
    int temp = octal;

    while(octal > 0) {

        //Calculation of remainder
        rem = octal % 10;

        //Calculation of octal value
        dec += (rem * pow(8,count));

        octal /= 10;

        count++;
    }

    //Restoring the binary variable
    octal = temp;

    printf("The octal of %d is %d", octal, dec);
}
Input:
Enter a octal Number : 121

Output:
The decimal of 121 is 81

Java

/* Java Program to convert Octal to Decimal */
//Save it as ConvertOctalToDecimal.java

import java.io.*;
import java.util.Scanner;

public class ConvertOctalToDecimal {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter a octal Number : ");
        int octal = scanner.nextInt();
        
                //Storing the value of binary to temporary variable
        int temp = octal;
        
        int rem, dec = 0, count=0;
        
        while(octal > 0) {
            
                        //Calculation of remainder
            rem = octal % 10;
            
                        //Calculation of octal value
            dec += (rem * Math.pow(8,count));
            
            octal /= 10;
            
            count++;
        }
        
                //Restoring the binary variable
        octal = temp;
        
        System.out.println("The decimal of "+ octal + " is "+ dec);
    }
}
Input:
Enter a octal Number : 
121

Output:
The decimal of 121 is 81

Related Programs

1) Program to convert Binary to Decimal
2) Program to convert Binary to Hexa Decimal
3) Program to convert Decimal to Octal
4) Program to convert Decimal to Binary
5) Program to find sum of digits of a number
6) Program to reverse a number
7) Program to calculate Factorial of a Number
8) Program to display multiplication table of a number
9) Program to Display Fibonacci Series
10) Program to Display Fibonacci Series using Recursion
11) Program of Simple Calculator using switch
12) Program to print all Squares of numbers from 1 to given Range
13) Program to find sum of first and last digit of a number
Share Me

Leave a Reply