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 : 




No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj