SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Decimal to Octal Conversion Program in Java and Python

21 August 2013

Decimal to octal conversion program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number in the Decimal number system and convert it into its equivalent number in the Octal number system.

Octal number system uses digits from 0 to 7. For example, decimal 25 is written as 31 in octal.

Decimal to octal conversion
Decimal to octal conversion is done by repeated division by 8.
INPUT: Enter a decimal number: 25 OUTPUT: Output = 31

Algorithm:

Step 1: Start.

Step 2: Accept decimal number n.

Step 3: If n is 0, display 0 and stop.

Step 4: Store octal digits 0 to 7 in a character array.

Step 5: Initialize result string s as blank.

Step 6: Repeat while n > 0.

Step 7: Calculate remainder r = n % 8.

Step 8: Add dig[r] before the current result string.

Step 9: Replace n by n / 8 using integer division.

Step 10: Display the octal result.

Step 11: Stop.

Explanation:

The program converts a decimal number to octal by using repeated division by 8. Octal is a base-8 number system, so every octal digit is obtained from the remainder left after division by 8. Each remainder will be from 0 to 7, which is directly a valid octal digit.

The variable r stores the current remainder. The string s stores the octal number being formed. The first remainder found represents the rightmost octal digit because it comes from the units place. Therefore, the statement that builds s places the new remainder before the previous contents of s. This is necessary because repeated division gives digits from right to left.

After storing a digit, the program divides n by 8 using integer division. This removes the octal place already processed and leaves the quotient for the next pass. The process continues until the quotient becomes 0. At that point, no more octal digits are left to find. The special case for n == 0 is handled separately, since zero should directly display as 0.

The logic is useful because it shows the connection between quotient, remainder and base conversion. Each division by 8 removes one octal place from the number. The remainder gives the digit for that place, while the quotient still contains the higher places that remain to be converted. This means the program is not memorising octal values; it is deriving them mathematically from the base of the number system. Storing each new digit in front keeps the final answer in normal left-to-right form.

Java Program:

Java
/**
* The class Dec2Oct inputs a Decimal number and converts it into its equivalent Octal number
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Dec2Oct
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a decimal number: ");
        int n = sc.nextInt();

        // If the entered number is 0, its octal form is also 0.
        if(n == 0)
        {
            System.out.println("Output = 0");
            return;
        }

        int r; // Stores the remainder obtained after division by 8.
        String s = ""; // Stores the final octal number.
        char dig[] = {'0','1','2','3','4','5','6','7'};

        // Continue division until all octal digits have been obtained.
        while(n > 0)
        {
            r = n % 8; // Remainder gives one octal digit.
            s = dig[r] + s; // Add at the front to correct the order.
            n = n / 8; // Move to the next quotient for further conversion.
        }

        System.out.println("Output = " + s);
    }
}

Equivalent Python Program:

Python
n = int(input("Enter a decimal number: "))

# Zero is printed directly because there are no remainders to collect.
if n == 0:
    print("Output = 0")
else:
    result = ""

    # Divide by 8 repeatedly to obtain octal digits from right to left.
    while n > 0:
        r = n % 8
        result = str(r) + result  # Add the latest remainder before previous digits.
        n = n // 8

    print("Output =", result)

Output:

Enter a decimal number: 465 Output = 721

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →