![]() Let’s print all the prime numbers between 1 to 100 package com.javainterviewpoint ![]() ("*** Printing the Prime Numbers ***") ĥ Prime Numbers between 1 to 100 / List of Prime Numbers from 1 to 100 Finally, iterate the array and pass each element to the checkPrime() method and perform the prime validation.Get the elements of the array from the user and store it in the array which is created in the previous step.Get the size of the array from the user and create an array to store the input numbers.In this approach, let’s get the input from the user and store it in the array and find all the prime numbers from array. ![]() } Java program to find all Prime Numbers from array We just need to make some minor modifications like the initialization of “i” happens just before the start of the loop, incrementation of “i” happens inside the loop and of course, we need to change for loop into while loop. We can also use the while loop instead of for loop, let’s re-write the above code using while loop. Prime Number Program in Java using While Loop If the number is greater than 1 and it is not divisible any number within the range of 2 to number/2 then it returns true.Returns false when the remainder is zero.Returns false when the number is less than or equal to 1.It returns boolean values based on the below criteria The checkPrime() method, checks whether the number passed is prime or not.Using Scanner get the input from the user and store it in the variable “number”.Scanner scanner = new Scanner(System.in) In an iterative loop, divide the number between the range 2 to number/2, and check if the remainder is not zero, if zero then the number is not a prime. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |