The factors of a positive integer number N are those numbers that divide N evenl
ID: 3831372 • Letter: T
Question
The factors of a positive integer number N are those numbers that divide N evenly. The factors of 14 are: 1, 2, 7, 14. Write a function countFactors that accepts a positive integer number as its parameter and returns the count of its factor as defined here (the call countFactors(14) returns 4). The function header is given. You must declare any local variables you need. Static int countFactors(final int n) { }// end function Write a main function that prompts for and reads an integer number . 1 from the user. It tells the user whether the number entered is a prime number or not. The main function must call the function from the previous question. It must not have a loop. Declare any local variables you need. public static void main (String[] args) { //declare variables necessary //prompt for and get the number from the user // Generate the output required, must use the given if statement if ( _____ ) out.println ("Your input number is prime else number is not prime") else out ("Your input number is not prime); } // end mainExplanation / Answer
static int countFactors(final int n) {
int count = 0;
for (int i = 1; i <= n; i++)
if (n%i == 0)
count ++;
return count;
}
public static void main(Sting[] args) {
//Declare
int n, count;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Prompt and get number
System.out.println("Enter a positive number: ");
n = Integer.parseInt(br.readLine());
//generate the required output
count = countFactors(n);
if (count > 2)
System.out.println("Not prime");
else
System.out.println("Not prime");
}
Tried my best to keep the code as simple as possible. Please let me know if you have any query regarding the code. I shall be glad to help you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.