Write a program to convert Decimal to Octal number system


The program starts by importing the Scanner class from the java.util package, which allows user input to be read from the console. Then a class named Decimal_Octal is defined, which contains the main method that executes the program.

Inside the main method, the program prompts the user to enter a decimal number using System.out.print and reads the input value using the Scanner class. After that, the decimal number is printed to the console using System.out.println.

The convert_dec_oct method is then called, passing the decimal number as a parameter. This method performs the conversion from decimal to octal using a while loop and arithmetic operations. Finally, the octal number is printed to the console using System.out.println.

Overall, this program demonstrates the use of basic Java syntax and concepts, such as variable declaration, user input/output, and method definition.

Source Code

import java.util.Scanner;  
class Decimal_Octal
{
        public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );
		System.out.print("Enter a Decimal Number : ");
		int dec =input.nextInt();
		System.out.println("Decimal Number : "+ dec);
		int octal = convert_dec_oct(dec);
		System.out.println("Octal Number : "+ octal);
    }
    public static int convert_dec_oct(int dec)
    {
        int octalNumber = 0, i = 1;
        while (dec != 0)
        {
            octalNumber += (dec % 8) * i;
            dec /= 8;
            i *= 10;
        }
        return octalNumber;
    }
}

Output

Enter a Decimal Number : 23
Decimal Number : 23
Octal Number : 27

Example Programs