import java.util.Scanner;
public class BinarytoDeci {
public static void main(String[] args) {
Scanner myScanner=new Scanner(System.in);
long num;
System.out.print("Enter a Binary number : ");
num=myScanner.nextInt();
int rem,sum=0,i=0;
while (num!=0) {
if(num==0){
break;
}else{
rem=(int) (num%10);
sum=(int) (sum+rem*(Math.pow(2, i)));
num=num/10;
i++;
}
}
System.out.println("Decimal value is : "+sum);
}
}
public class BinarytoDeci {
public static void main(String[] args) {
Scanner myScanner=new Scanner(System.in);
long num;
System.out.print("Enter a Binary number : ");
num=myScanner.nextInt();
int rem,sum=0,i=0;
while (num!=0) {
if(num==0){
break;
}else{
rem=(int) (num%10);
sum=(int) (sum+rem*(Math.pow(2, i)));
num=num/10;
i++;
}
}
System.out.println("Decimal value is : "+sum);
}
}
No comments:
Post a Comment