Convert The Given Binary Number into Decimal Number by using Java Program
Code:
import java.util.*;
import java.lang.*;
public class BinToDec
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("\nHow Many Digits : ");
int n=sc.nextInt();
int[] a = new int[n];
int d = 0,p = n-1;
System.out.print("\nEnter The Binary Number : ");
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
for(int j=0;j<n;j++)
{
int q = (int)Math.pow(2,p);
d = d + a[j]*q;
p--;
}
System.out.println("\nDecimal Number : "+d+"\n");
}
}
Output of program:
import java.util.*;
import java.lang.*;
public class BinToDec
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("\nHow Many Digits : ");
int n=sc.nextInt();
int[] a = new int[n];
int d = 0,p = n-1;
System.out.print("\nEnter The Binary Number : ");
for(int i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
for(int j=0;j<n;j++)
{
int q = (int)Math.pow(2,p);
d = d + a[j]*q;
p--;
}
System.out.println("\nDecimal Number : "+d+"\n");
}
}
Output of program:
No comments