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…

Check prime or not

C++ example to check  a Number is Prime or Not

To understand this example programs, you should have previous knowledge of following C++ topics

For loop in C++ language

While loop in C++ language

Do while loop in C++ language

If statements in C++ language

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

Data type in C++ language

Variable in C++ language

The operator 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

 

Example program to check whether a Number is Prime or Not in Java
C programming code to check whether the character is Alphabet or not
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago