in ,

Java Program To Find The Largest Prime Number Among N Numbers

Find Largest Prime Among N Numbers

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.

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
60falseInteger.MIN_VALUE
6falseInteger.MIN_VALUE
90falseInteger.MIN_VALUE
23truetrue23
11truefalse23

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
20falseInteger.MIN_VALUE
30falseInteger.MIN_VALUE
54falseInteger.MIN_VALUE
60 false Integer.MIN_VALUE
70 false Integer.MIN_VALUE

Thus, the output is: NO PRIME NUMBERS FOUND!

VARIABLE DESCRIPTION

METHOD NAMEVARIABLE NAMEDATA TYPEPURPOSE
isPrime()
aintTo store the number that is to be checked whether prime or not.
iintUsed as the control variable of a for-loop.
cintUsed as a counter variable to count the number of factors of a number.
main()
nintTo accept the no. of numbers that are to be inputted by the user.
iint Used as the control variable of the for-loop.
maxint To store the largest prime number among the inputted N no.s.
aintTo store the N no.s inputted by the user, one by one.

ALGORITHM

  1. START
  2. Including the ‘Scanner’ class from the ‘util’ package of java.
  3. Define class LargestPrime
  4. Define a function isPrime that takes an int as its parameter and returns a boolean.
  5. Define int variables i and c. Set c=0.
  6. REPEAT FOR I=1 TO A.
    6.1. Check if(a is divisible by i) then set c=c+1.
  7. END OF FOR I.
  8. return true is c equals to 2, else return false.
  9. End of isPrime function.
  10. Define the main method.
  11. Define an object ‘sc’ of the Scanner class with ‘System.in’ as its parameter.
  12. Define an object ‘lp’ of the LargestPrime class.
  13. Define int variables (i, n, max, a). Set max=Integer.MIN_VALUE.
  14. Ask the user how many numbers she/he wants to enter.
  15. Ask the user to enter N numbers.
  16. 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).
  17. End of FOR I.
  18. 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.
  19. End of the main method.
  20. End of class.
  21. 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!

What do you think?

106 Points
Upvote Downvote

Written by Anirban Roy

Anirban is a full-stack developer and an SEO expert. He has 6 years of experience in web development and software engineering. With a passion for writing, he has been blogging since 2017.

Leave a Reply

Your email address will not be published. Required fields are marked *