Showing posts with label binary-to-decimal. Show all posts
Showing posts with label binary-to-decimal. Show all posts

Wednesday, August 5, 2020

Write a program to input a number in Binary form and convert it Decimal form? (1010 in binary then 10 in decimal, 2^0 , 2^1……)

73)  Write a program to input a number in Binary form and convert it Decimal form? (1010 in binary then 10 in decimal, 20 , 21……)


import java.util.*;
public class Binary_to_decimal
{
  public static void main(String args[ ])
  {
           Scanner sc = new Scanner (System.in);
           System.out.println("Enter a BINARY number");
           int b = sc.nextInt();
           int a = b;
           int m = 0;
           int r = 0;
           while(a>0)
           {
               int d = a%10;
               int x = (int)Math.pow(2,m);
               r = r+(x*d);
               m++;
               a=a/10;
           }
           System.out.println(b+" in DECIMAL = "+r);
  }
  }


Output : 




 jhvghvgk ;nkjn ,nkjhj