Question

Using java, Write a method named factor that shows the factors of 2 in an integer....

Using java,

Write a method named factor that shows the factors of 2 in an integer. While the user enters 0 as a number, the loop must exit


For example,

Call    Output from your method call

twoFactors (7) 7 = 7

twoFactors (18) 18 = 2*9

twoFactors (68) 68 = 2*2*17

twoFactors (120) 120 = 2*2*2*15

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class TwoFactors {

    public static void twoFactors(int n) {
        System.out.printf("twoFactors (%d) %d = ", n, n);
        while (n % 2 == 0) {
            System.out.printf("2*");
            n /= 2;
        }
        System.out.println(n);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num;
        while(true) {
            num = in.nextInt();
            if(num == 0) {
                break;
            }
            twoFactors(num);
        }
    }

}

tw°Factors (7) 7 = 7 18 twoFactors (18) 182*9 68 twoFactors (68) 682*2*17 120 twoFactors (120) 120 2*2*2*15 Process finished

Add a comment
Know the answer?
Add Answer to:
Using java, Write a method named factor that shows the factors of 2 in an integer....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming Write a method named isEven that accepts an integer argument. The method should return...

    Java Programming Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the...

  • Write a method named printGrid that accepts two integer parameters rows and cols. The output is...

    Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • Write a Java application that asks for an integer and returns its factorization into prime factors...

    Write a Java application that asks for an integer and returns its factorization into prime factors including their powers. The prime factors must appear in increasing order. For example if 120 (= 2*2*2*3*5) in the input number, then your program should output 120 = 2^3 * 3^1 * 5^1 The factors should appear in increasing order and should be separated from each other by asterisks (*).

  • Write a class named DiceGame.java that contains a method called diceSum that prompts the use for...

    Write a class named DiceGame.java that contains a method called diceSum that prompts the use for a desired sum, then repeatedly simulates the rolling of 2 six-sided dice until their sum is the desired sum (use a do/while loop). Here is a sample dialogue with the user: Desired dice sum: 9 4 and 3 = 7 3 and 5 = 8 5 and 6 = 11 5 and 6 = 11 1 and 5 = 6 6 and 3 =...

  • Write a method named printGrid that accepts two integer parameters rows and cols. The output is...

    Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column...

  • Write a method named printGrid that accepts two integer parameters rows and cols. The output is...

    Write a method named printGrid that accepts two integer parameters rows and cols. The output is a comma-separated grid of numbers where the first parameter (rows) represents the number of rows of the grid and the second parameter (cols) represents the number of columns. The numbers count up from 1 to (rows x cols). The output are displayed in column-major order, meaning that the numbers shown increase sequentially down each column and wrap to the top of the next column...

  • Write a Java program that contains a method named ReadAndFindMax. The argument to this method is...

    Write a Java program that contains a method named ReadAndFindMax. The argument to this method is the filename (of String type). The method opens the file specified in the method argument filename and then reads integers from it. This file can contain any number of integers. Next, the method finds the maximum of these integers and returns it. The main method must first ask the user to enter the filename; then call ReadAndFindMax method with the entered filename as an...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT