C++ example to check whether a Number is Prime or Not
- Home
- Check value
- C++ example to check whether a Number is Prime or Not
- On
- By
- 0 Comment
- Categories: Check value
C++ example to check whether a Number is Prime or Not
C++ example to check whether a Number is Prime or Not
In this tutorial, we will discuss the concept of C++ example to check whether a Number is Prime or Not
In this post, we are going to learn how to check a number is prime or not using for, while and do-while loop in C++ programming language with Example program
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…
C++ example to check a Number is Prime or Not
To understand this example programs, you should have previous knowledge of following C++ topics
Check the prime number using for loop
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num,i,count=0; cout<<"Enter the positive integer\n"; //Takes input from user cin>>num; for(i=2; i<=num/2; i++){ //condition for non-prime if(num%i==0) { count=1; break; } } if(num==1){ cout<<"you entered"<<num<<"\n"; cout<<num<<" is neither a prime nor a composite number "; } else{ if(count==0){ cout<<"you entered" <<num<<"\n\n"; cout<<num<<" is a prime number "; } else{ cout<<"you entered: " <<num<<"\n\n"; cout<<num<<" is not a prime number "; } } getch(); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter the positive integer 45 you entered: 45 45 is not a prime number
Case 2
Enter the positive integer 43 you entered: 43 43 is a prime number
Case 3
Enter the positive integer 1 you entered 1 i is neither a prime nor a composite number
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
Check the prime number using while loop
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int num,i,count=0; cout<<"Enter the positive integer\n"; //Takes input from user cin>>num; i=2; while(i<=num/2){ //condition for non-prime if(num%i==0) { count=1; break; } i++; } if(num==1){ cout<<"you entered "<<num<<"\n"; cout<<num<<" is neither a prime nor a composite number "; } else{ if(count==0){ cout<<"you entered" <<num<<"\n\n"; cout<<num<<" is a prime number "; } else{ cout<<"you entered" <<num<<"\n\n"; cout<<num<<" is not a prime number "; } } getch(); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter the positive integer 99 you entered: 99 99 is not a prime number
Case 2
Enter the positive integer 97 you entered: 97 97 is a prime number
Case 3
Enter the positive integer 1 you entered 1 i is neither a prime nor a composite number
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
Check the prime number using the do-while loop
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int num,i,count=0; cout<<"Enter the positive integer\n"; //Takes input from user cin>>num; i=2; do{ //condition for non-prime if(num%i==0) { count=1; break; } i++; }while(i<=num/2); if(num==1){ cout<<"you entered "<<num<<"\n"; cout<<num<<" is neither a prime nor a composite number "; } else{ if(count==0){ cout<<"you entered" <<num<<"\n\n"; cout<<num<<" is a prime number "; } else{ cout<<"you entered" <<num<<"\n\n"; cout<<num<<" is not a prime number "; } } getch(); return 0; }
When the above code is executed, it produces the following results
Case 1
Enter the positive integer 190 you entered: 190 190 is not a prime number
Case 2
Enter the positive integer 199 you entered: 199 199 is a prime number
Case 3
Enter the positive integer 1 you entered 1 i is neither a prime nor a composite number
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
Suggested for you
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