Question

In: Computer Science

Must be in Java Write a program called showTwos that shows the factors of 2 in...

Must be in Java

Write a program called showTwos that shows the factors of 2 in a given positive integer (user input) in the range of [16,128]. For example, if user inputs 7, 18, 68, 120, the correctly working program should print the following:

7 = 7

18 = 2 * 9

68 = 2 * 2 * 17

120 = 2 * 2 * 2 * 15

Solutions

Expert Solution

For this we are firstly checking if the number is not in range of 16 to 128 ,then no two's multiples shown. If it is in range then we find how many 2's are there using % which gives remainder. So we check if number%2==0 means if it is fully divisible by 2 then increase count else stop the loop. We use this count to print multiples of 2

import java.util.Scanner;
public class showTwos {
   public static void main(String[] args){
       Scanner scan=new Scanner(System.in);
       System.out.print("Enter positive integer between 16 to 128 ");
       int number=scan.nextInt();
       if((number<16)||(number>128)){
           System.out.println(number+"="+number);
       }
       else {
           int n = number;
           int count = 0;
           while (n > 0) {

               if (n % 2 == 0) {
                   count++;
                   n = n / 2;
               } else {
                   break;
               }
           }
           String res = "2*";
           for (int i = 1; i < count; i++) {
               res = res + "2*";
           }

           System.out.println(number + "=" + res + n + "  " + count);

       }
   }
}

Related Solutions

This program must be done in JAVA. Write a program to monitor the flow of an...
This program must be done in JAVA. Write a program to monitor the flow of an item into an out of a warehouse. The warehouse has numerous deliveries and shipments for this item (a widget) during the time period covered. A shipment out (an order) is billed at a profit of 50% over the cost of the widget. Unfortunately each incoming shipment may have a different cost associated with it. The accountants of the firm have instituted a last-in, first...
In Java INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the...
In Java INSTRUCTIONS Write a java program called InputValidation.java that will use Scanner to ask the computer user to enter his/her: • Name: a string representing the user’s first name • month of birthday: an integer representing the month of the user’s birthday • day of birthday: an integer representing the day of the user’s birthday • year of birthday: an integer representing the year of the user’s birthday Consider the following aspects that your program shall perform: VALIDATE USER’S...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Java Programing Write a program called reverseProg.   This program will do the following Asks the user...
Java Programing Write a program called reverseProg.   This program will do the following Asks the user to input a string, it reads the string and does the followings Prints the string in reverse order. Displays the characters that are in position 7th, 8th and 9th of this string Displays the length of the string Displays the string in all UPPER CASE Sample output example: Enter a string: Wilmington University String Entered is: Wilmington University Wilmington University spelled backward is: ytsirevinU...
2 Write a Java program called Pattern that prints an 8-by-8 checker box pattern using an...
2 Write a Java program called Pattern that prints an 8-by-8 checker box pattern using an if loop.
Write a java program that will read a file called stateinfo.txt and will store the information...
Write a java program that will read a file called stateinfo.txt and will store the information of the file into a map. The stateinfo.txt file contains the names of some states and their capitals. The format of stateinfo.txt file is as follows. State                Capital ---------                         ----------- NSW               Sydney VIC                 Melbourne WA                 Perth TAS                 Tasmania QLD                Brisbane SA                   Adelaide The program then prompts the user to enter the name of a state. Upon receiving the user input, the program should...
Create a complete java program called Week_Report. The program must include two array structures, a string...
Create a complete java program called Week_Report. The program must include two array structures, a string array called DaysOfWeek and a double array called Temp_Values. Store in the DaysOfWeek array the following values (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday). Store in the Temp_Values array the following (23.5, 34.0, 20.9, 45.7, 29.3, 34.5, 32.5). Using a for loop structure output the values for the two arrays. Day of the Week Temperature Values Monday 23.5 Tuesday 34.0 Wednesday 20.9 Thursday 45.7...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT