Write C++ program to check whether a number is Prime number or not using while loop

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
    int num, i, f;
 
    //Reading a number from user
    cout<<"Enter any number:";
    cin>>num;
 
    f = 0;
    i = 2;
    while(i <= num/2)
    {
        if(num%i == 0)
        {
            f=1;
            break;
        }
        i++;
    }
    if(f == 0)
 
        cout<<num<<" is a Prime Number"<<endl;
    else
 
        cout<<num<<" is Not a Prime Number"<<endl;
 
   return 0;
}
 

Result

Write C++ program to check whether a number is Prime number or not using while loop
Write C++ program to check whether a number is Prime number or not using while loop

 

Leave a Comment