Q:

Java program to convert Decimal to Binary

0

Java program to convert Decimal to Binary

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Core Java Example programs for Beginners and Professionals

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to convert Decimal to Octal... >>
<< Java program to find addition of N integer numbers...