Factor Finder / Prime Number Checker

 

Modules - Structured Programming 1, Structured Programming 2, Procedural Programming 1

 

Teacher’s Notes:

========================================================================

In this exercise, students will have the user enter a number and then (a) print out it's factors and also, state whether it is prime or not.  This can be done with just a straight program or it could also be built into methods/functions to practice procedural programming.

Technically, the FOR loop could check from 1 to the square root of the number for factors, but I am using from 1 to the number because the program requires you to print the factors in ascending order.

To make this trickier, you can have the output print the numbers with commas separating the factors.  This is marginally trickier because the last factor does not have a comma after it.

 

Assignment:

========================================================================

A number (n) is considered to be a prime number if it can only be divided by 1 and itself.  A common activity in CS and mathematics is to check if the number is a prime number and also to find its factors.  Write a program that will prompt the user to enter a positive integer.  The program will then print out the factors of the number from 1 to n, in ascending order.  The program will also state whether the number is prime or not.
 
SAMPLE INPUT 1:
 
Please enter a positive integer
16
 
SAMPLE OUTPUT 1:
 
Factors:
1,2,4,8,16
This is not a prime number

 

SAMPLE INPUT 2:

Please enter a positive integer
131

SAMPLE OUTPUT 2:

Factors:
1,131
This is a prime number

 

Solution (JAVA):

========================================================================

 

 

import java.util.Scanner;
public class FactorFinder {
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        
        //prompting for, and reading input
        System.out.println("Please enter a positive integer");
        int number = input.nextInt();
        int factorCount = 0;
        
        System.out.println("Factors:");
        
        //**Note that no scope brackets are required for the FOR loop here because it technically 
        //only contains one command - the IF statement.
        for(int x = 1; x <= number; x++) // looping from 1 to the number 
            if(number % x == 0){ //if the remainder of the division is zero, the number is prime
                factorCount++;//add 1 to our count of the factors
                System.out.print(x);//print the factor
                if(x != number)//if the factor is not the number, it's not the last factor so...
                    System.out.print(",");//...we need to print a comma to separate it from the next factor
            }
        System.out.println();
        if(factorCount > 2)//if there are more than two factors...
            System.out.println("This is not a prime number");//...it's not a prime number
        else//otherwise...
            System.out.println("This is a prime number");//...it's a prime number
    }
}