We’ve declared two variables lower and upper and assigning those variables with user input. In the above code, we are initializing a scanner for reading user input. Let’s build the code now! Scanner sc=new Scanner(System.in) The whole algorithm will be almost similar to the above code, the only difference we make is taking user input for the lower limit and upper limit of the range. After running the prime number algorithm for each number we are pushing it into ArrayList if it is a prime number. Now we have two for loops first for loop is for looping over all the numbers between 1 to 100 and the second for loop is our previous prime number algorithm. In the above code, we’ve declared an ArrayList that stores all the prime numbers in the range of 1 to 100. Here’s the code to do that.Ĭheck out upGrad’s Java Bootcamp int n = 5 If the count is 2 then we can conclude that the given number is a prime, else it is not a prime. At first, we need to loop over all the numbers from 1 to N and maintain a count of numbers that properly divides the given number. Before jumping to the code, we’ll understand the algorithm to check if a number is a prime number or not.