In this coding example, we are going to check the prime numbers within a range of numbers. We enter a minimum number as 20 and the maximum number as 50, and then we find the prime numbers within that range. 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.
The coding example is shown below. Code Explanation: In the above code, we find the number of prime numbers between 20 and We find the numbers which are divisible only by unity and the number itself. The numbers which are prime are 23, 29, 31, 37, 41, 43, and If the number is divisible, it is a composite number, and if the number is not divisible, it is a prime number. In this article, we see the prime numbers working using a for loop and a while loop.
Also, we see the prime numbers which are present within a certain range. The logic used for checking a prime number using a for loop and a while loop is almost the same. So, the checking of prime numbers is quite easy. The loop control variable is a very important factor in checking whether a number is prime or not.
This is a guide to Prime Numbers in Java. Here we discuss the introduction and different examples of prime numbers in java along with code implementation. You may also look at the following articles to learn more-. Submit Next Question. By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password? If you want to know more about taking inputs in Java, Read this: Taking input from users in Java. Now, this program checks for the number of integers, th e number is completely divisible.
And the value of the number of factors is stored in the count named variable. As we all know Prime numbers have only 2 factors i. We can observe that the factors of the number starts getting repeating itself. Hence our approach now got faster. So now if the number is divisible by any of the number then the count variable is incremented.
Find the standard deviation. Reference Materials String. Start Learning Java. Explore Java Examples. Java Examples Check Leap Year. Check Whether a Number is Positive or Negative. Check Whether a Character is Alphabet or Not. Calculate the Sum of Natural Numbers.
Find Factorial of a Number. Generate Multiplication Table. Display Fibonacci Series. Related Topics Java ArrayList clone. Java while and do To understand this example, you should have the knowledge of the following Java programming topics: Java while and do
0コメント