The factors of a positive integer number are those numbers that divide N evenly.
ID: 3831387 • Letter: T
Question
The factors of a positive integer number are those numbers that divide N evenly. The factors of 14 are:1, 2, 7, 14. Write a function copuntFactors accepts a positive integer number as its parameters and returns the count of its factors 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 ( __________ ) out.println ("Your input number is prime"); else out println ("Your input number is not prime"); }//end mainExplanation / Answer
Find the Answers below. I mentioned the program code with output.
2.
import java.util.Scanner;
public class CountFactors {
public static void main(String[] args) {
System.out.println("Enter the N");
Scanner s = new Scanner(System.in);
int N = s.nextInt(); //To get the input from the user
System.out.println(countFactors(N)); //Calling the countFactors method
}
static int countFactors(final int n){
int count = 0;
if(n > 0){
for (int i = 1; i <= n; i++) {
if(n % i ==0){ //To check if the number gives modulo 0 or not
count++;
}
}
}
return count;
}
}
OUTPUT:
Enter the N
14
4
3. import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
System.out.println("Enter the Number");
Scanner s = new Scanner(System.in);
int n = s.nextInt(); // To get the input from the user
if (n > 1) {
int m = n / 2;
for (int i = 2; i <= m; i++) {
if (n % i != 0) {
System.out.println("Your input number is prime");
} else {
System.out.println("Your input number is not prime");
}
break;
}
}
}
}
OUTPUT:
1. Enter the Number
10
Your input number is not prime
2. Enter the Number
7
Your input number is prime
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.