The project of this assignment is to how to do c comparisons

The project of this assignment is to how to do c comparisons using the if statement and passing arguments into main through line, Additionally, you get more practice on how to write all of your own code javadoc comments. Problem Description The detailed description of this problem comes from the programming projects p6.3. that is in a book(page 302) Ignore the last paragraph that starts. You are writing of this problem that prints out all the factors of a number, in ascending order. All the work of the program, as usual, will be in Factors .java. In You should have the user enter a single integer. Then pass that integer to a method you write, In that method you will find all the factors, in ascending order, and add them to a single string with a single space between each number. Including the last. Then return that string from the method. Getting Started We are going to do this exercise by writing code that prints the factors of a given number in Factors java. How to start Every project in this class create a source file called Factors.java. This is where your code will go. There is no code for the assignment. You get to do it all Don\'t forget to provide proper javadoc documentation. To run your own tests while developing, in ask the user for input(using a scanner) and outputs the factors sequentially. If you input 150 then your output should look like this. Notice that the numbers are in ascending order. You will need to do that too.

Solution

Please find the required solution:

import java.util.Scanner;

public class FactorGenerator {

   //Implementation of the find factors
   public static String findFactors(int n) {
       StringBuilder builder = new StringBuilder();

       // iterate until the number is even
       while (n % 2 == 0) {
           builder.append(\"2\").append(\" \");
           n = n / 2;
       }

       // n is odd here
       for (int i = 3; i <= Math.sqrt(n); i = i + 2) {
           // while the odd number is divided
           while (n % i == 0) {
               builder.append(i).append(\" \");
               n = n / i;
           }
       }

       // handle the prime numbers
       if (n > 2)
           builder.append(n).append(\" \");
       return builder.toString();
   }


   // Test class input and invoke method find factors
   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in);
       System.out.print(\"Enter the number greater than 2:\");
       int input = keyboard.nextInt();
       System.out.println(findFactors(input));
       keyboard.close();
   }
}

Sample output:

Test-1:

Enter the number greater than 2:315
3 3 5 7

Test-2:

Enter the number greater than 2:999
3 3 3 37

 The project of this assignment is to how to do c comparisons using the if statement and passing arguments into main through line, Additionally, you get more pr
 The project of this assignment is to how to do c comparisons using the if statement and passing arguments into main through line, Additionally, you get more pr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site