Program to find prime number
In this post, you will learn how to write programmes in different languages to print prime numbers.
A prime number is a whole number greater than 1, whose only factors are 1 and itself, like -2, 3, 5, 7, 11, etc. For example, 17 is a prime number because it is only divisible by 1 and 17, on the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9, and number itself.
Java print prime numbers from 1 to 20 using for loop
Here, we have used for loop to get prime numbers from 1 to 20. We loop over the number range (1 to 20) and check whether the number is prime or not. The for loop iterates from i=0 to i=given number, if the remainder of i/num = 0, then it increases the count by 1. After all the iterations, if counter=2, then that number is a prime number.
public class CheckPrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
String prime_numbers = "";
for (i = 1; i <= 20; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
prime_numbers = prime_numbers + i + " ";
}
}
System.out.println("Prime Numbers between 1 and 20 :");
System.out.println(prime_numbers);
}
}
Output of the above code:
Prime Numbers between 1 and 20 :
2 3 5 7 11 13 17 19
PHP Prime Number Program using While Loop
The following PHP code prints a list of prime numbers between 1 and 100 (that is, numbers not divisible by something other than 1 or the number itself) using a while loop.
In the given code, we have two while loops. The inner while loop does the testing with each possible divisor. If the inner loop finds a divisor, the number is not prime, so it breaks out without printing anything, and if the testing gets as high as the square root of the number, we can assume that the number is prime. The outer loop works through all the numbers between 1 and 100. This loop is broken when we have reached the breaking point of numbers to test.
<?php
$limit = 30;
$init = 2;
while(TRUE)
{
$div = 2;
if($init > $limit)
{
break;
}
while(TRUE)
{
if($div > sqrt($init))
{
echo $init." ";
break;
}
if($init % $div == 0)
{
break;
}
$div = $div + 1;
}
$init = $init + 1;
}
?>
Output of the above code:
2 3 5 7 11 13 17 19 23 29
Python Program to print Prime Numbers between interval
Python programme that uses a for loop to print prime numbers between a given interval. Numbers less than or equal to 1 are not prime numbers. That's why, we have started the range from 1.
for num in range(1, 50):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Output of the above code:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
C++ program to print Prime Number
The given C++ program takes the range and finds all the prime numbers between the range and also prints the number of prime numbers.
#include<iostream>
using namespace std;
//function to check for prime number
void prime(int num)
{
int div=0;
//checking for number of divisor
for(int i=1;i<=num;i++)
{
if(num%i==0)
div++;
}
if(div==2)
cout<<num<<endl;
}
int main()
{
cout<<"Please enter range: ";
int min, max;
cin>>min>>max;
cout<<"Prime numbers between "<<min<<" and "<<max<<" are:"<<endl;
//finding prime numbers in the given range
for(int i=min;i<=max;i++)
prime(i);
return 0;
}
Output of the above code:
Please enter range: 20 40
Prime numbers between 20 and 40 are:
23
29
31
37
C Program to Find Prime Numbers in a given Range
The given C program takes the range and finds all the prime numbers between the range and also prints the number of prime numbers.
#include <stdio.h>
int main() {
int min, max, i, flag;
printf("Please enter range: ");
scanf("%d %d", &min, &max);
printf("Prime numbers between %d and %d are: ", min, max);
// iteration until low is not equal to high
while (min < max) {
flag = 0;
// ignore numbers less than 2
if (min <= 1) {
++min;
continue;
}
// if low is a non-prime number, flag will be 1
for (i = 2; i <= min / 2; ++i) {
if (min % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", min);
++min;
}
return 0;
}
Output of the above code:
Please enter range: 50 70
Prime numbers between 50 and 70 are: 53 59 61 67
Related Articles
Java program to find area of triangleArea of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to convert Decimal to Octal
C program to convert decimal to binary
C program to check whether a number is even or odd