Binary to decimal conversion of numbers FAROOQ
import java.util.*;
public class day_2
{
//converting a number from binary to decimal
public static void binaryToDecimal(int binnum){
int temp=binnum;
int decimal=0; //initialize number final to be getting in
int pow=0; //first power of 2 is 0 then increment
while(binnum>0){ //whil loop better dont know no. of iterations
int rem=binnum%10; //pehle last ka digit nikalo to multiply it with powers of 2
decimal=decimal +(rem * (int)Math.pow(2,pow)); //main step val me add karte jao
pow++; //power update
binnum/=10; //remove the last digit
}
System.out.println("The decimal value of "+temp+" is "+ decimal);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in) ;
{ System.out.println("Enter binary to covert into decimal");
int n=sc.nextInt();
int temp=n;
boolean valid=true;
while(temp>0)
{
int rem=temp%10;
if(rem!=0&&rem!=1){
valid=false;
}
temp/=10;
}
if(valid==true)
{
binaryToDecimal(n);
}else{
System.err.println("Please input only 0's and 1's");
}
}
}
}