Write a Program Convert Binary To Decimal in Java- Technology369kk

 Convert Binary To Decimal

Program


import java.util.*;
class Binary_Decimal{
    Scanner scan;
    int num;
   

    void getVal() {  // This function gets the value of the binary number
        System.out.println("Binary to Decimal:");
        scan = new Scanner(System.in);

        System.out.println("\nEnter the binary number:");
        num = Integer.parseInt(scan.nextLine(), 2);  // Correct method name and usage
    }

    void convert() {  // Converts the binary value to decimal and prints it
        System.out.println("Decimal Value is: " + num);
    }

    public static void main(String args[]) {
        Binary_Decimal obj = new Binary_Decimal();
        obj.getVal();   // Correct placement of function calls
        obj.convert();
    }
}



Output: 


PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\allpraque> java Binary_Decimal
Binary to Decimal:

Enter the binary number:
10101
Decimal Value is: 21



Post a Comment

0 Comments