In: Computer Science
Write a program that takes an integer as input and returns whether it is a prime number or not in C++. (Using if, loops, or/and bool only)
the output of the above
program is:
code for copy:
#include<iostream>
using namespace std;
int main()
{
//declare the integer variable n
int n;
//declare boolean variable b and assign true to
it
bool b=true;
//enter the value of n
cout<<"enter the value of n:";
//read the value of n
cin >> n;
//for loop takes range from 2 to n-1
for(int i=2;i<n;i++)
{
//if any of the number between 2 to
n-1 is divisible by then the number is not prime
if(n%i==0)
{
//print the
given number is not prime.
cout<<"The
number is not prime."<<endl;
//make
bool=false if the number is not prime
b=false;
break;
}
}
//if b is equal to true then the number is
prime.
if(b==true)
{
//print the given number is
prime
cout<<"The number is
prime."<<endl;
return 0;
}
}
I HOPE THIS WILL HELP YOU....