Home » Java solved programs » Core java programs

Java program to convert Decimal to Binary

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

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

// Scanner class is used for taking input from user
import java.util.Scanner;

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

		System.out.println("Enter Any Decimal Number :");
		//Accept input from user
		int input_decimal_num = sc.nextInt();

		String binary_string = " ";

		//Loop continues till input_decimal_num >0
		while(input_decimal_num > 0){
			//remainder add to string variable 
			binary_string = input_decimal_num%2 + binary_string;
			input_decimal_num = input_decimal_num/2;
		}
		// Display Final Result 
		System.out.println("Conversion of decimal to binary is : " + binary_string);
	}
}

Output

D:\Java Articles>java DecimalToBinaryConversionClass
Enter Any Decimal Number :
30
Conversion of decimal to binary is : 11110




Comments and Discussions




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.