Convert Octal to Decimal in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C - Convert Octal to Decimal */
#include <stdio.h>
#include <math.h>
int main(){
    long long oct; scanf("%lld",&oct);
    long long dec=0, base=1;
    while(oct>0){
        int d = oct%10;
        dec += d*base;
        base *= 8;
        oct /= 10;
    }
    printf("%lld", dec);
    return 0;
}

C Output

Input:  
157

Output:  
111


C++ Program

// C++ - Convert Octal to Decimal
#include <bits/stdc++.h>
using namespace std;
int main(){
    long long oct; cin>>oct;
    long long dec=0, base=1;
    while(oct>0){
        int d = oct%10;
        dec += d*base;
        base *= 8;
        oct /= 10;
    }
    cout<<dec;
}

C++ Output

Input:  
245  

Output:  
165


JAVA Program

// Java - Convert Octal to Decimal
import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    long oct = s.nextLong(), dec = 0, base = 1;
    while(oct > 0){
      long d = oct % 10;
      dec += d * base;
      base *= 8;
      oct /= 10;
    }
    System.out.print(dec);
  }
}

JAVA Output

Input:  
10

Output:  
8


Python Program

# Python - Convert Octal to Decimal
octal = int(input())
decimal, base = 0, 1
while octal > 0:
    d = octal % 10
    decimal += d * base
    base *= 8
    octal //= 10
print(decimal)

Python Output

Input:  
77 

Output:  
63


In-Depth Learning – Entire Concept in Paragraphs
Example
For input 157 (octal), the program displays 111 (decimal) because:
1×8² + 5×8¹ + 7×8⁰ = 64 + 40 + 7 = 111. Each digit is raised to a power of 8, beginning at the rightmost digit at power 0, and then the results are added.

Real-Life Analogy
Suppose you have a pile of boxes stacked in tiers, where one tier can carry 7 boxes (since octal numbers range from 0 to 7). The lowest tier is worth 1 box per unit, the second is worth 8 boxes per unit, the third is worth 64 boxes per unit, and so on. Adding up all boxes as a whole is similar to going octal to decimal — you're simply adding up the amount depending on each position's value.

Why It Matters
Base conversions are essential in computer science since systems of numbers interpret numbers differently. Octal numbers appear in UNIX/Linux permissioning (such as 755 for permissions to a file) and in ancient computing where they fit exactly into binary. Knowing how to manually convert bases strengthens an understanding of how computers store and operate on numbers.

Learning Insights
This code instructs you in processing a number digit by digit from right to left through modulus (%) and integer division (/ or //). The multiplication by an increasing base (8th powers) demonstrates the functionality of positional number systems. You also get to know how to deal with various types of data (such as long long in C/C++ and long in Java) in order to store larger results without causing harm.
Interview Relevance and Practical Use
Interviewers can ask this to test your knowledge of number systems and loops. A related follow-up is to convert from other bases such as binary, decimal, and hexadecimal or do the conversion without using library functions. In actual programming, such reasoning comes handy while parsing raw data formats, interpreting machine code, or dealing with legacy systems.

Performance and Edge Notes
Time complexity is O(d) and d represents the number of digits in the octal number. Space complexity is O(1) because no additional storage is needed. This method will work for any valid octal number. For invalid digits (8 or 9), add input validation in production code.

SEO-friendly summary
This C, C++, Java, and Python program for converting an octal number to decimal performs the conversion without relying on built-in functions for conversion. Through the processing of every digit and the multiplication by the appropriate power of 8, the logic illustrates how positional numbering systems function. This is a concept that is important for coders studying base conversions, UNIX file permission systems, and representations of numbers in computing and thus should be an essential practice for coding interviews as well as core computer science education.