prime or not

C++ code to check whether the number is prime or not

C++ code to check whether the number is prime or not

In this tutorial, we will discuss the C++ code to check whether the number is prime or not

In this post, we are going to learn how to check whether the given number is prime or not  using 5 ways in C++ language

This is done using for loop, while loop, do-while loop function and recursion

 

A prime number is a number which is greater than(positive) one and divisible by only two numbers: 1 and it self. when any number is divisible by any other number it is not a prime number.

So, prime numbers can,t be divided by other numbers than itself or 1 Eg 2,3,5,7,11,13,17 ………..

C++ Code to check whether the number is prime or not

Program to check whether the number is prime or not -using for loop

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,i,count=0;
    cout<<"Enter a positive integer\n";
    cin>>num;

    for(i=1; i<=num; ++i){
        if(num%i==0){
            count++;

        }
    }

    if(num==1){
        cout<<"1 is neither prime or composite";
    }
    else{
         if(count==2){
        cout<<num<<" is a prime number";
    }
    else{
        cout<<num<<" is not a prime number";
    }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
109
109 is a prime number

Case 2

Enter a positive integer
111
111 is not a prime number

Case 3

Enter a positive integer
1
1 is neither prime or composite

 

In this program,

  • integer variable num,i  are declared.
  • integer variable count=0 is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store to the variable num
  • Create a for loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to 2 or not
  • when the if statement returns true then the count value is increased by 1 until the test condition becomes false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is composite
  • Then,the program is displayed the output using cour<< statements.

 

 

Program to check whether the number is prime or not -using while loop

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,i,count=0;
    cout<<"Enter a positive integer\n";
    cin>>num;
i=1;
    while(i<=num){
        if(num%i==0){
            count++;

        }
        ++i;
    }

    if(num==1){
        cout<<"1 is neither prime or composite";
    }
    else{
         if(count==2){
        cout<<num<<" is a prime number";
    }
    else{
        cout<<num<<" is not a prime number";
    }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
211
211 is a prime number

Case 2

Enter a positive integer
207
201 is not a prime number

Case 3

Enter a positive integer
1
1 is neither prime or composite

 

In this program,

  • integer variable num,i  are declared.
  • integer variable count=0 is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store to the variable num
  • Create a while loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to 2 or not
  • when the if statement returns true then the count value is increased by 1 until the test condition becomes false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is composite
  • Then,the program is displayed the output using cour<< statements.

 

 

Program to check whether the number is prime or not -using do- while loop

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,i,count=0;
    cout<<"Enter a positive integer\n";
    cin>>num;
i=1;
   do{
        if(num%i==0){
            count++;

        }
        ++i;
    } while(i<=num);

    if(num==1){
        cout<<"1 is neither prime or composite";
    }
    else{
         if(count==2){
        cout<<num<<" is a prime number";
    }
    else{
        cout<<num<<" is not a prime number";
    }
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
503
503 is a prime number

Case 2

Enter a positive integer
507
507 is not a prime number

Case 3

Enter a positive integer
1
1 is neither prime or composite number

 

In this program,

  • integer variable num,i  are declared.
  • integer variable count=0is declared and initialized
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store to the variable num
  • Create a do-while loop of i from 1 to n and increase the value of i after every iteration by 1
  • Calculate mod of every value of i and check whether the result is equal to 2 or not
  • when the if statement returns true then the count value is increased by 1 until the test condition becomes false
  • The given numbers are tested whether it is prime or not using the count value
  • if the value of the count is equal to 0 then the number is prime else  the number is composite
  • Then,the program is displayed the output using cout<< statement.

 

 

Program to check whether the number is prime or not -using function

Program 4

#include <iostream>
#include <conio.h>
using namespace std;
int primeNum(int);//function declaration
int main()
{
    int num,result=0;
    cout<<"Enter a number\n";
    cin>>num;
    result=primeNum(num);//function call
    if(result==0)
         cout<<num<<" is a prime number";
    else
        cout<<num<<" is not a prime number";
               getch();
    return 0;
}
    int primeNum(int n)//function definition
    {
        int i;
        for(i=2; i<=n/2; i++)
        {
            if(n%2!=0)
                continue;
            else
                return 1;
        }
        return 0;
    }

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
57
57 is a prime number

Case 2

Enter a positive integer
34
34 is not a prime number


Program to check whether the number is prime or not -using recursion

Program 5

#include <iostream>
#include <conio.h>
using namespace std;

int isPrime(int,int);//function prototype
int main()
{
    int num,result;
    cout<<"Enter a positive number\n";
    cin>>num;
    result=isPrime(num,num/2);
    if(result==1)
         cout<<num<<" is a prime number";
    else
        cout<<num<<" is not a prime number";
               getch();
    return 0;
}
    int isPrime(int n,int i)
    {
            if(i==1)
                return 1;
            else{
                    if(n%i==0)
                return 0;
            else
                isPrime(n,i-1);

            }
    }

When the above code is executed, it produces the following result

Case 1

Enter a positive integer
67
67 is a prime number

Case 2

Enter a positive integer
56
56 is not a prime number

 

Suggested post you

For loop in c++ language

while loop in C++ language

Do-while in C++ language

If statements in C++ language

Operator in C++ language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.