Updated March 23, 2023
Introduction to Prime Numbers in C
A prime number is a finite numerical value that is higher than 1, and that can be divided only by 1 and itself. A few of the prime numbers starting in ascending order are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. In C programming, there are a few possible operations involving the prime numbers like ‘to find if the given number is a prime number or not’, ‘to display all the prime numbers inside a given range’, ‘to display the prime numbers below a specific value’, and ‘to display the prime numbers above a specific value’. These scenarios can be coded in C programming using the conditional statements and looping statements, such as for loop, if else condition and while loop.
Examples to Implement Prime Numbers in C
In this section, we are going to discuss a few programs to check prime numbers using C language.
Example #1
Program to check prime number in C using for loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i, count = 0, m;
printf("Enter the number: ");
scanf("%d",&num);
m = num / 2;
for(i = 2; i <= m; i++)
{
if(num % i == 0)
{
printf("Entered number is not prime");
printf("\n");
count = 1;
break;
}
}
if(count == 0)
{
printf("Entered number is prime");
printf("\n");
}
return 0;
}
Output:
Code Explanation:
Here we have written a program to check prime number using for loop. We have used four variables, variable num is used to allow a user to enter the value. Variable i is used to check the condition, variable count is used to set a counter value. and variable m is used to check the mathematical calculation.
Example #2
Program to check prime number in C using while loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i = 2, count = 0;
printf("Enter the number: ");
scanf("%d",&num);
while(i <= sqrt(num))
{
if(num % i == 0)
{
count = 1;
break;
}
i++;
}
if(count == 0)
{
printf("Entered number is prime");
}
else
{
printf("Entered number is not prime");
printf("\n");
}
return 0;
}
Output:
Code Explanation:
Here we have written a program to check prime number using while loop. Here we have used three variables num, i and count. The #include<math.h> library is used to perform mathematical functions. In this program, we make use of the sqrt() function to find out the square root of the number.
In this program, first, it asks a user to enter a number. Then the entered number is copied into num. Here num is used to compare the result with the original. while condition checks whether the number is greater than 0 or not. If the number is greater than 0, it will execute the statements following while. Then it will check for the condition num % i == 0.
Example #3
Program to check prime number in C using a do while loop.
Code:
#include<stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
int num, i = 2, count = 0;
printf("Enter the number: ");
scanf("%d",&num);
do
{
if(num % i == 0)
{
count = 1;
break;
}
i++;
}
while(i <= sqrt(num));
if(count == 0)
{
printf("Entered number is prime");
printf("\n");
}
else
{
printf("Entered number is not prime");
printf("\n");
}
return 0;
}
Output:
Code Explanation:
Here we have written a program to check prime number using a do-while loop. Here we have written a program to check prime number using a do-while loop. Here we have used three variables num, i and count. The #include<math.h> library is used to perform mathematical functions. In this program, we make use of the sqrt() function to find out the square root of the number.
In this program, first, it asks a user to enter a number. Then the entered number is copied into num. Here num is used to compare the result with the original. while condition checks whether the number is greater than 0 or not. if the number is greater than 0, it will execute the statements following while. Then it will check for the condition num % i == 0. The only difference in the above example that it first checks the condition i.e. i <= sqrt(num) and here in this example the same condition is tested at the end of the loop.
Example #4
Program to print prime number between two intervals in C using while loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int lower_limit, upper_limit, i, count;
printf("Enter the lower limit: ");
scanf("%d",&lower_limit);
printf("Enter the upper limit: ");
scanf("%d",&upper_limit);
printf("List of prime numbers between " );
printf("%d",lower_limit);
printf(" and ");
printf("%d",upper_limit);
printf("\n");
while(lower_limit < upper_limit)
{
count = 0;
for(i = 2; i <= lower_limit/2; ++i)
{
if(lower_limit % i == 0)
{
count = 1;
break;
}
}
if(count == 0)
printf("%d",lower_limit);
printf("\n");
++lower_limit;
}
return 0;
}
Output:
Recommended Articles
This is a guide to Prime Numbers in C. Here we discuss what is prime number along with programs to check whether the number is prime or not using various loops. You may also have a look at the following articles to learn more –