Binary to decimal
Send Feedback
Given a binary number as an integer N, convert it into decimal and print.
Input format :
Output format :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner( System.in );
int binarynum =in.nextInt();
int binary=binarynum;
int decimal = 0;
int power = 0;
while(true)
{
if(binary == 0)
{
break;
}
else
{
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
System.out.println(decimal);
}
}
No comments:
Post a Comment