Prime Number or Not in C Program using While, Do-while, For loop, and Function
ADVERTISEMENTS
Prime number or not in c program.
In this article, you will learn how to check whether the given input number is a prime number or not a prime number.
To solve this problem we will use the various approaches like while loop
, do-while loop
, for loop
, function
, etc.
Example-1
INPUT A NUMBER: 37
37 IS PRIME NUMBER
37 IS PRIME NUMBER
Example-2
INPUT A NUMBER: 40
40 IS NOT A PRIME NUMBER
40 IS NOT A PRIME NUMBER
You should know about the following topics in C programming to understand this program:
- If-else statement
- For loop in C
- While loop in C
- Do-while loop in C
- Function in C
1 - Prime Number or Not in C using While loop
// Prime Number or Not in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num;
int is_prime = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check if the input number is less than or equal to 1
if (num <= 1)
is_prime = 1;
// Step-3 Use a loop to check whether the input number is prime or not
int i = 2;
while (i <= num / 2)
{
// If the input number is divisible by i, then the input number is not a Prime
if (num % i == 0)
{
is_prime = 1;
break;
}
i++;
}
// Step-4 If `is_prime == 0` INPUT number is prime
// Final output of the program
if (is_prime == 0)
printf("%d IS A PRIME NUMBER.\n", num);
else
printf("%d IS NOT A PRIME NUMBER.\n", num);
return 0;
}
Output
INPUT A NUMBER: 37
37 IS PRIME NUMBER
2 - Prime Number or Not in C using Do-while loop
// Prime Number or Not in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num;
int is_prime = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check if the input number is less than or equal to 1
if (num <= 1)
is_prime = 1;
// Step-3 Use a loop to check whether the input number is prime or not
int i = 2;
do
{
// If the input number is divisible by i, then the input number is not a Prime
if (num % i == 0)
{
is_prime = 1;
break;
}
i++;
} while (i <= num / 2);
// Step-4 If `is_prime == 0` INPUT number is prime
// Final output of the program
if (is_prime == 0)
printf("%d IS A PRIME NUMBER.\n", num);
else
printf("%d IS NOT A PRIME NUMBER.\n", num);
return 0;
}
3 - Prime Number or Not in C using For loop
// Prime Number or Not in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num;
int is_prime = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check if the input number is less than or equal to 1
if (num <= 1)
is_prime = 1;
// Step-3 Use a loop to check whether the input number is prime or not
for (int i = 2; i <= num / 2; i++)
{
// If the input number is divisible by i, then the input number is not a Prime
if (num % i == 0)
{
is_prime = 1;
break;
}
}
// Step-4 If `is_prime == 0` INPUT number is prime
// Final output of the program
if (is_prime == 0)
printf("%d IS A PRIME NUMBER.\n", num);
else
printf("%d IS NOT A PRIME NUMBER.\n", num);
return 0;
}
Output
INPUT A NUMBER: 40
40 IS NOT A PRIME NUMBER.
4 - Prime Number or Not in C using Function
// Prime Number or Not in C using For loop
#include <stdio.h>
// Function to check if it is a prime or not
int is_prime(int num)
{
// If the input number is divisible by i, then the input number is not a Prime
for (int i = 2; i < num; i++)
{
// If the input number is divisible by i, It's not a prime number
if (num % i == 0)
return 0;
}
// If there are no divisors, It's a prime number
return 1;
}
// It's the main function of the program
int main()
{
int num;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check if the input number is less than or equal to 1
if (num <= 1)
printf("%d IS NOT A PRIME NUMBER.\n", num);
else
{
// Step-3 Call the is_prime() function to check whether the input number is prime or not
if (is_prime(num))
printf("%d IS A PRIME NUMBER.\n", num);
else
printf("%d IS NOT A PRIME NUMBER.\n", num);
}
return 0;
}
Output
INPUT A NUMBER: 40
40 IS NOT A PRIME NUMBER.