Java Program To Convert Decimal To Binary

[8217 views]




What is Decimal Number?

A decimal number system is a number with base 10 numeral system. It's the most commonly used number system. The decimal number system consists of 10 digits i.e 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

What is Binary number?

A binary number is a number which is expressed in the base 2 numeral system. A Binary Number only consists of 0's and 1's.
Example of a binary number is :
101010

Java Program To Convert Decimal To Binary

Decimal to Binary Conversion Java Program using Arrays

class DecimaltoBinary { public static void main(String args[]) { System.out.println("Enter a decimal number"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); // initializing an array with size 100 for storing binary number int binary[]=new int[100]; int i = 0; while(n > 0) { binary[i++] = n%2; n = n/2; } System.out.print("Binary number is : "); for(int j = i-1;j >= 0;j--) { System.out.print(binary[j]); } } }

Output

Enter a decimal number
4
Binary number is : 100

Decimal to Binary Conversion Java Program using Recursion

import java.util.*; class DectoBin { int binary[]=new int[100]; int i = 0; void binary(int num) { if(num>0) { binary[i++] = num%2; num = num/2; binary(num); } for(i=i-1;i >= 0;i--) { System.out.print(binary[i]); } } public static void main(String arg[]) { DectoBin d=new DectoBin(); System.out.println("Enter a decimal number"); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); System.out.print("Binary number is : "); d.binary(n); } }

Output

Enter a decimal number
54
Binary number is : 110110

                 





Comments








Search
Need any Help?


Hot Deals ends in





Earn Money While You Sleep. Join Now to avail Exclusive Bonus




Technical Quiz:



Search Tags

    Decimal to Binary Conversion using Java

    Java Code to convert Decimal to Binary

    Decimal to Binary using Recursion