Convert The Given Decimal Number into Binary Number by using Java Program
Code:
import java.util.*;
public class DecToBin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Decimel Number : ");
int d = sc.nextInt();
int[] a = new int[100];
int n = 0,s = 0;
do
{
a[n] = d%2;
d = d/2;
n++;
}while(d != 0);
for(int i=0;i<n/2;i++)
{
s = a[i];
a[i] = a[n-1-i];
a[n-1-i] = s;
}
System.out.print("\nBinary Number : ");
for(int i=0;i<n;i++)
{
System.out.print(a[i]);
}
System.out.println("\n");
}
}
Output of program:
import java.util.*;
public class DecToBin
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter The Decimel Number : ");
int d = sc.nextInt();
int[] a = new int[100];
int n = 0,s = 0;
do
{
a[n] = d%2;
d = d/2;
n++;
}while(d != 0);
for(int i=0;i<n/2;i++)
{
s = a[i];
a[i] = a[n-1-i];
a[n-1-i] = s;
}
System.out.print("\nBinary Number : ");
for(int i=0;i<n;i++)
{
System.out.print(a[i]);
}
System.out.println("\n");
}
}
Output of program:
No comments