Question

In: Computer Science

JAVA: Write a program that converts a binary number to decimal (integer) Based on user input:...

JAVA:

Write a program that converts a binary number to decimal (integer)

Based on user input: Ask user to input a binary number

Conditions:

Check that the binary number starts with a 1 and only has 0s and 1s

If condition is not met, ask for user to reenter a number

Conver the binary to decimal (integer)

Ask user if they want to input another number if not then the program will exit

*Please do not use built-in java functions when converting binary to decimal

Solutions

Expert Solution

Please go through screenshots for reference .And check line numbers according to line numbers in screenshot you can easily understand.Have any doubts comment below.Thank you.

JAVA CODE:

import java.util.*;
public class BinaryToDecimal {
   public static void main(String args[])
   {
       System.out.print("Enter Binary number:");
       BinaryToDecimal.TakeInput();  
   }
   public static void TakeInput() //Method to Take input from user
   {
       Scanner sc = new Scanner(System.in);
       String number = sc.next(); //Taking input from user
      
       if(number.charAt(0)=='1') // Condition to check number starts with 1 or not
       {
           int n = Integer.parseInt(number);
           int l = 0;
           while(n>0)       // Loop to check number having only 0's or 1's
           {
               if(n%10==0 || n%10==1)
                   l++;
               n=n/10;
           }
           if(l==number.length()) // if it is binary number pass the number to convert to decimal
           {
               BinaryToDecimal.ToDecimal(Integer.parseInt(number));
           }
           else // number is not binary ask user to reenter the number
           {
               System.out.print("reenter a number:");
               BinaryToDecimal.TakeInput();  
           }
       }
       else // if number is not start with 1 ask user to reenter the number
       {
           System.out.println("reenter a number:");
           BinaryToDecimal.TakeInput();  
       }
   }
   public static void ToDecimal(int num) // method to change the binary number to decimal
   {
       int decimal=0,p=0;
       while(num>0)
       {
           decimal += (num%10)*Math.pow(2,p);
           num = num/10;
           p++;
       }
       System.out.println(decimal);
       System.out.print("If you want to input another number(y/n):"); // Ask user if you to enter another number
       Scanner sc1 = new Scanner(System.in);
       char ch = sc1.next().charAt(0);   // scaning character to y-for YES and n-for NO
      
       if(ch=='n') // if n (NO) EXIT form program
       {
           System.exit(0);
       }
       else // if y (YES) ask user to enter number
       {
           System.out.print("Enter the Binary number:");
           BinaryToDecimal.TakeInput();
       }      
   }
}


Related Solutions

Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Write a program that prompts the user to input a decimal number and outputs the number...
Write a program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer.
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT