Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Enter any number:19

19 is a Prime Number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to check whether a number is pal... >>
<< Write C++ program to calculate compound Interest...