Prime Number Program in C: Prime numbers are number that should be divided by itself or by “1” only and should be greater than “1”.
2, 3, 5, 7, 11 etc. are considered as prime numbers as they are divisible by them self and by “1” only.
Contents
- 1 Prime Number Program in C Algorithm:
- 2 Prime number program in c using for loop:
- 3 Prime number program in C using while loop:
- 4 C program to print prime numbers in a given range
- 5 Prime numbers from 1 to 100 in c:
- 6 nth prime number in c
- 7 Sum of prime numbers in c
- 8 C program to count number of prime numbers in a given range
- 9 Prime number series program in c
- 10 C program to print prime numbers from 1 to n using for loop
- 11 C program to find prime number using function
- 12 Twin prime numbers between 1 to 100 in c
- 13 Prime factor program in c
- 14 C program to find prime numbers in an array
- 15 C program to display prime numbers between two intervals
- 16 Consecutive prime sum in c
- 17 Prime number program in c using do while loop
- 18 C program to print a triangle of prime numbers up to a given number of lines of the triangle
- 19 Prime number program in c using recursion
- 20 Find next prime number in c
- 21 Circular prime number program in c
- 22 C program to print alternate prime numbers
- 23 Prime number program in c using array
Prime Number Program in C Algorithm:

logic for prime number in c
Prime number program in c using for loop:
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int number,a,b=0,flag=0;
printf("Please enter the number that you have to check prime or not");
scanf("%d",&number);
b=number/2;
for(a=2;a<=b;a++)
{
if(number%a==0)
{
printf("%d is not a prime number", number);
flag=1;
break;
}
}
if(flag==0)
printf("%d is a prime number",number);
getch();
}
Output:
Please enter the number that you have to check prime or not 3
3 is a prime number
Please enter the number that you have to check prime or not 4
4 is not a prime number
In order to understand how the for loop works then check for loop in c with examples
Prime number program in C using while loop:
To check whether the given number is a prime number or not, present we are using while loop and iterate from 2 to half of the given number.
If any number is divisible other than itself or “1” then we can exit from the while loop in C.
Here is the example of while loop.
In this example we have taken num, a, b, c as variables.
Variables b, c are initialized to “0” and a as “2”.
If num % a==0 then the given number is not prime and the program terminates from the loop.
Example of Prime Number Program in C using While loop:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num,a,b=0,c=0;
printf("Please enter the number that you have to check prime or not");
scanf("%d",&num);
b=num/2;
a=2;
while(a<=b)
{
if(num%a==0)
{
printf("%d is not a prime number",num);
c=1;
break;
}
a++;
}
if(c==0)
printf("%d is a prime number",num);
getch();
}
Output:
Please enter the number that you have to check prime or not 5
5 is a prime number
Please enter the number that you have to check prime or not 10
10 is not a prime number
C program to print prime numbers in a given range
Prime numbers from 1 to 100 in c:
nth prime number in c
Sum of prime numbers in c
C program to count number of prime numbers in a given range
Prime number series program in c
C program to print prime numbers from 1 to n using for loop
C program to find prime number using function
Twin prime numbers between 1 to 100 in c
Prime factor program in c
C program to find prime numbers in an array
C program to display prime numbers between two intervals
Consecutive prime sum in c
Prime number program in c using do while loop
C program to print a triangle of prime numbers up to a given number of lines of the triangle
Prime number program in c using recursion
Find next prime number in c
Circular prime number program in c
C program to print alternate prime numbers
Prime number program in c using array
Using C