Binary to Decimal
Given a binary number as an integer N, convert it into decimal and print.
Sample Input 1 :
Sample Output 1 :
1100
12
import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int i=0; int s=0; while(n!=0) { int d=n%10; int b= d* (int)Math.pow(2,i); s=s+b; n=n/10; i++; } System.out.println(s); } }
Comments
Post a Comment