Tech Study

C++: To Check A Number Is Prime Or Not Using While,For 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

C++ program to check number is positive, negative or zero

C++ Program to check whether two matrices are equal or not

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

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more