
In this article, I will show you how to find the largest prime number among N inputted Integers. This program also prints “No Prime Number Found”. If none of the inputted Integers is prime.
I have solved this problem without using an array.
I have also included the dry run, variable description and algorithm along with the program.
Contents
What Is A Prime Number?
Any number that is only divisible by 1 and the number itself, is called a prime number. For example 2, 3, 5, 7, 13, 11, etc. are all prime numbers.
Thus, we can see that a prime number has exactly 2 factors: 1 and itself.
LOGIC EXPLANATION
In this program, we first initialize a variable called ‘max’ with the smallest possible Integer variable, using Integer.MIN_VALUE. Then, we accept the Integers from the user and simultaneously check whether or not it is a prime number using the user-defined function isPrime that should be defined to return a boolean value. If the Integer is prime, we compare it with the ‘max’ variable and if it is greater than ‘max’, we update the value of ‘max’ with that number.
The value that is remained is ‘max’ is the required out. However, if the ‘max’ value is the same as Integer.MIN_VALUE, that means none of the inputted numbers was prime.
JAVA PROGRAM
import java.util.Scanner;
class LargestPrime{ //opening of the class.
boolean isPrime(int a){ //returns true if the number is prime, else returns false.
int i,c=0;
for(i=1;i<=a;i++) //this loop is used to count the number of factors of the number.
if(a%i==0)
c++;
return c==2;
}
public static void main(String[] args){ //opening of the main method.
Scanner sc=new Scanner(System.in);
LargestPrime lp=new LargestPrime();
int n,i,max=Integer.MIN_VALUE,a;
System.out.println("HOW MANY NUMBERS?");
n=sc.nextInt();
System.out.println("ENTER "+n+" INTEGERS");
for(i=0;i<n;i++){
a=sc.nextInt(); //accepting integers from the user
if(lp.isPrime(a))
max=Math.max(max,a);
}
if(max!=Integer.MIN_VALUE)
System.out.println("LARGEST PRIME: "+max);
else
System.out.println("NO PRIME NUMBERS FOUND!");
} //closing of the main method
} //closing of class
DRY RUN
CASE 1
SUPPOSE, THE VALUE OF N IS 5.
Let the 5 integers be:-
60
6
90
23
11
a | isPrime(a) | a>max | max |
60 | false | Integer.MIN_VALUE | |
6 | false | Integer.MIN_VALUE | |
90 | false | Integer.MIN_VALUE | |
23 | true | true | 23 |
11 | true | false | 23 |
Thus, the output is: LARGEST PRIME: 23
CASE 2
SUPPOSE, THE VALUE OF N IS 5.
Let the 5 integers be:-
20
30
54
60
70
a | isPrime(a) | a>max | max |
20 | false | Integer.MIN_VALUE | |
30 | false | Integer.MIN_VALUE | |
54 | false | Integer.MIN_VALUE | |
60 | false | Integer.MIN_VALUE | |
70 | false | Integer.MIN_VALUE |
Thus, the output is: NO PRIME NUMBERS FOUND!
VARIABLE DESCRIPTION
METHOD NAME | VARIABLE NAME | DATA TYPE | PURPOSE |
isPrime() | |||
a | int | To store the number that is to be checked whether prime or not. | |
i | int | Used as the control variable of a for-loop. | |
c | int | Used as a counter variable to count the number of factors of a number. | |
main() | |||
n | int | To accept the no. of numbers that are to be inputted by the user. | |
i | int | Used as the control variable of the for-loop. | |
max | int | To store the largest prime number among the inputted N no.s. | |
a | int | To store the N no.s inputted by the user, one by one. |
ALGORITHM
- START
- Including the ‘Scanner’ class from the ‘util’ package of java.
- Define class LargestPrime
- Define a function isPrime that takes an int as its parameter and returns a boolean.
- Define int variables i and c. Set c=0.
- REPEAT FOR I=1 TO A.
6.1. Check if(a is divisible by i) then set c=c+1. - END OF FOR I.
- return true is c equals to 2, else return false.
- End of isPrime function.
- Define the main method.
- Define an object ‘sc’ of the Scanner class with ‘System.in’ as its parameter.
- Define an object ‘lp’ of the LargestPrime class.
- Define int variables (i, n, max, a). Set max=Integer.MIN_VALUE.
- Ask the user how many numbers she/he wants to enter.
- Ask the user to enter N numbers.
- REPEAT FOR I=0 TO N.
16.1. Store the user’s input in the variable ‘a’.
16.2. Check if(isPrime(a)) then, set max=Math.max(max,a). - End of FOR I.
- Check if(max is not equals to Integer.MIN_VALUE)
18.1. Then display that max is the required output.
18.2. Else display that none of the inputted integers was prime. - End of the main method.
- End of class.
- STOP.
I hope this article of mine helped you understand and solve the problem of accepting N integers and then printing the largest prime number among them. If you have any confusion, feel free to comment down below. I’ll try my best to help you out!
If you liked this article, kindly consider sharing it with your friends, because you know, sharing is caring!
Have a great day ahead!