Java and C Program to Convert Octal to Decimal

Posted in

Java and C Program to Convert Octal to Decimal
vinaykhatri

Vinay Khatri
Last updated on December 10, 2024

    Here in this article, we have provided Java and C source codes, that ask the user to enter a number in decimal and convert it to the Decimal number system.

    Example:

    Input: Octal: 011
    Output: Decimal: 9
    
    Input: Octal: 20
    Output: Decimal: 16
    
    Input : Octal: 40
    Output: Decimal: 32

    Octal Number System

    Octal number system has a base of 8 and it represents its number using 0 to 7.

    Decimal Number System

    In decimal number system we have 10 digits 0 to 9 to represent a number.

    Logic: Octal to Decimal

    There is an easy logic to convert an Octal number to Decimal.

    • Take an octal number which must be a combination of numbers including [0-7].
    let's take an octal number
    75
    
    
    • Extract the number from left to right digit by digit.
    Extract 75 from left to right.
    7 and 5.
    
    
    • Multiply the extracted numbers with 8 to the power of number unit.
    7 is at 1st position so it would be multiplied with 8^1
    5 is at 0 position so it would be multiplied with 8^0
    7 * (81)=56  and 5 * (80)=5
    • Now add them all.
    56 + 5 =61

    Another example:

    Octal                       134
                              /  |  \
                             /   |   \
                            1    3    4
                          /      |     \
                         /       |      \
                     1*(82) + 3*(81) + 4*(80)  
                         \       |      /
                          \      |     /
                           64 + 24  + 4 
                            \    |   /
                             \   |  /
    Decimal                     92                       

    Java Program to Convert Octal to Decimal

    import java.io.*;
    import java.util.Scanner;
    import java.lang.Math;
    class Tech {
    //Conversion method
    static int oct_to_dec(int n)
    {
        int dec_num = 0;
        // The unit value is 0 for the last digit
        int unit = 0; 
        int temp = n;
        int digit;
        while (temp > 0) { 
          // Extract the Octal number from last digit
           digit = temp % 10;
           temp = temp / 10; 
           //multiply the digit with 8 to the power base unit
           dec_num += digit * Math.pow(8, unit);
           unit = unit +1;
        }
        return dec_num;
    } 
    public static void main(String[] args)
    {
        Scanner sc= new Scanner(System.in);
        System.out.print("Octal: ");
        int num= sc.nextInt();
        System.out.println("Decimal: "+oct_to_dec(num));
        }
    }

    Output:

    Octal: 1575
    Decimal: 893

    C Program to Convert Octal to Decimal

    using namespace std; 
    #include<stdio.h>
    #include<math.h>
    int oct_to_dec(int n)
        { 
            int dec_num = 0; 
            // The unit value is 0 for the last digit
            int unit = 0; 
            int temp = n;
            int digit;
            while (temp > 0) {
                // Extract the Octal number from last digit
                digit = temp % 10;
                temp = temp / 10; 
                //multiply the digit with 8 to the power base unit
                dec_num += digit * pow(8, unit);
                unit = unit +1;
            }
            return dec_num;
        } 
    int main()   
    {
        int n;
        printf("Octal:");
       scanf("%d", &n);
       printf("Decimal:%d ",oct_to_dec(n));
    return 0;
    }

    Output:

    Octal:2345
    Decimal:1253

    Python Program to Convert Octal to Decimal

    def oct_to_dec(n):
    	dec_num =0
    	unit =0
    	temp =n
        
    	while(temp>0):
    		digit = temp%10
    		temp =temp//10
    		dec_num+=digit*pow(8,unit)
    		unit+=1
    	return dec_num
    
    
    n= int(input("Octal: "))
    print("Decimal:", oct_to_dec(n))

    Output:

    Octal: 23456
    Decimal: 10030

    People are also reading:

    Leave a Comment on this Post

    0 Comments