![]() The loop control variable is a very important factor in checking whether a number is prime or not. So, the checking of prime numbers is quite easy. The logic used for checking a prime number using a for loop and a while loop is almost the same. Also, we see the prime numbers which are present within a certain range. ![]() In this article, we see the prime numbers working using a for loop and a while loop. If the number is divisible, it is a composite number, and if the number is not divisible, it is a prime number. The code uses a single for loop, which is used to check the number’s divisibility by the respective for loop control variable. We find the numbers which are divisible only by unity and the number itself. The coding example is shown below.Ĭode Explanation: In the above code, we find the number of prime numbers between 20 and 50. It is a very easy program, and just by changing the min and max variables, we can find the prime numbers between the min and the max variables. We enter a minimum number as 20 and the maximum number as 50, and then we find the prime numbers within that range. In this coding example, we are going to check the prime numbers within a range of numbers. We enter 71 and 37 as two numbers and then finally find out that both the numbers are prime numbers as they are divisible by only 1 and the number itself. Public static void main(String args)throws IOExceptionĬode Explanation: In the sample output, we input two odd numbers to check whether the numbers are prime or not. We use the same logic as the for loop, but we have a different outlook on the program. In the coding example, we are going to see the usage of the While loop for checking whether a number is prime or not. It is an even number that is divisible by 2, 29 other than unity and the number itself. Finally, we find out that 58 is not a prime number, but it is a composite number. We enter 58 as the number, and then we check whether the number is prime or not. Second, we enter another number to check whether the number is prime or not. Except for that, it is not divisible by any other number. We find out that 29 is a prime number as it is divisible by only the number of unity and the number itself. First, we enter 29 as a number to check whether it is prime or not. (num + " is not a prime number.") Ĭode Explanation: In the output, we see whether an inputted number is prime or not. ![]() Int num= Integer.parseInt(br.readLine()) ("ENTER A NUMBER TO CHECK IF IT IS PRIME OR NOT") Public static void main(String args) throws IOExceptionīufferedReader br= new BufferedReader(new InputStreamReader(System.in)) Other than that, there are meaningful names used in the program for anyone reading the program to understand. We have given the throws IO Exception command after the main’s declaration, which throws an exception that occurs during the input/output operation. Also, if there is an IOException, then it is treated differently. In the program, the java.io.* is imported so that there are input/output operations appearing in the following code lines. The code and the part of the loop are shown below, which inputs a number and gives the respective output whether that number is a prime number or not. Then we have a variable that reports if the number is divisible or not by any number, which is a part of them loop. The for loop starts from 2, and then the loop goes on till half the respective number. Then we have a for loop in which we are going to check the divisibility of the number by any other number except for 1 and any other number. We first input the number using Buffered Reader Stream input. ![]() After all the numbers have been checked, print the value of the counter.In the first coding example, we are going to check whether a number is prime or not. Then when you encounter a prime number in the if block, instead of printing it, increment the value of the counter. Just declare a variable of type integer, like say counter and initialize its value to zero. Since, you want to count the number of primes generated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |