In this article, we will discuss the concept of the Example program to check whether a Number is Prime or Not in C programming language
In this post, we are going to learn how to check whether a number is prime or not from the given number using for, while and do-while loop in C language
Prime number
The number that can be divided by 1 and itself. it is called prime number
for Example 2,3,5,7,11,13…
To understand this example programs, you should have previous knowledge of following C topics
Program 1
This program allows the user to enter a positive number and then it will check the given number is a prime number or not using for loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int num,i,count=0; printf("Enter the positive integer\n"); scanf("%d",&num); for(i=2; i<=num/2; i++){ //condition for non-prime if(num%i==0) { count=1; break; } } if(num==1){ printf("you entered %d\n",num); printf("%d is neither a prime nor a composite number ",num); } else{ if(count==0){ printf("you entered %d\n\n",num); printf("%d is a prime number ",num); } else{ printf("you entered %d\n",num); printf("%d is not a prime number ",num); } } getch(); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter the positive integer 1 you entered 1 1 is neither a prime nor a composite number
Case 2
Enter the positive integer 38 you entered 38 38 is not a prime number
Case 3
Enter the positive integer 29 you entered 29 29 is a prime number
Program 2
This program allows the user to enter a positive number and then it will check the given number is a prime number or not using while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int num,i,count=0; printf("Enter the positive integer\n"); scanf("%d",&num); i=2; while(i<=num/2){ //condition for non-prime if(num%i==0) { count=1; break; } i++; } if(num==1){ printf("you entered %d\n",num); printf("%d is neither a prime nor a composite number ",num); } else{ if(count==0){ printf("you entered %d\n\n",num); printf("%d is a prime number ",num); } else{ printf("you entered %d\n",num); printf("%d is not a prime number ",num); } } getch(); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter the positive integer 1 you entered 1 1 is neither a prime nor a composite number
Case 2
Enter the positive integer 20 you entered 20 20 is not a prime number
Case 3
Enter the positive integer 13 you entered 13 13 is a prime number
Program 3
This program allows the user to enter a positive number and then it will check the given number is a prime number or not using the do-while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int num,i,count=0; printf("Enter the positive integer\n"); scanf("%d",&num); i=2; do{ //condition for non-prime if(num%i==0) { count=1; break; } i++; }while(i<=num/2); if(num==1){ printf("you entered %d\n",num); printf("%d is neither a prime nor a composite number ",num); } else{ if(count==0){ printf("you entered %d\n\n",num); printf("%d is a prime number ",num); } else{ printf("you entered %d\n",num); printf("%d is not a prime number ",num); } } getch(); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter the positive integer 1 you entered 1 1 is neither a prime nor a composite number
Case 2
Enter the positive integer 12 you entered 12 12 is not a prime number
Case 3
Enter the positive integer 7 you entered 7 7 is a prime number
Suggested for you
input-output function in C language
Similar post
Program to check whether a Number is Prime or Not in C++
Program to check whether a Number is Prime or Not in C
Program to check whether a Number is Prime or Not in Java
Program to check whether a Number is Prime or Not in Python
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…