15.9k views
0 votes
Write a program that tests whether an integer is a prime number.

1 Answer

0 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int n;

cin>>n;//taking input form the user.

bool b=true;

for(int i=2;i<n;i++)//looping

{

if(n%i==0)//checking that i divides n.

{

cout<<"The number is not prime";

b=0;

break;

}

}

if(b)

{

cout<<"The number is prime";

}

return 0;

}

Input:-

23

Output:-

The number is prime.

Step-by-step explanation:

The above written program is in C++.First I have declared an integer n.Then taking input form the user and storing it in n abd also declared a boolean variable with value true.Then I have used a for loop for checking n is divisible by any number from 2 to n-1 if it is then printing the message of not prime,making the value of b to false and coming out of the loop.out of the loop if the value of the b is true then printing the number is prime.

User Kiprainey
by
7.6k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.