Home » Java solved programs » Core java programs

Java program to convert Decimal to Octal

In this java program, we are going to learn how to convert Decimal to Octal? Here, we will read an integer (Decimal) number and convert it into its equivalent Octal number.
Submitted by Preeti Jain, on March 11, 2018

Given an Integer (Decimal) number and we have to convert it into Octal using java program.

import java.util.Scanner;

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

		System.out.println("Enter Any Decimal Number :");
		//Accept input from keyboard
		int input_decimal_num = sc.nextInt();
		int octal_num = 0,rem,temp_input_decimal_num,i=1;
		temp_input_decimal_num = input_decimal_num;

		//Loop continues till temp_input_decimal_num to 0
		while(temp_input_decimal_num > 0){
			//Get remainder
			rem = temp_input_decimal_num%8;
			octal_num = octal_num+rem*i;
			temp_input_decimal_num = temp_input_decimal_num/8;
			i=i*10;
		}
		//Display Result 
		System.out.println("Conversion of decimal to octal is : " + octal_num);
	}
}

Output

D:\Java Articles>java DecimalToOctalConversionClass
Enter Any Decimal Number :
222
Conversion of decimal to octal is : 336






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.