c++ A prime number is an integer greater than 1 that is evenly divisible by only
ID: 674028 • Letter: C
Question
c++
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can be evenly divided only by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.
Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate the function in a complete program. (Hint: you will need a loop that checks the remainders from dividing the given number N by all numbers starting from 2 and up to N-1).
Comment
Explanation / Answer
#include<iostream.h>
#include<conio.h>
bool isPrime(int a)
{
int i,c;
clrscr();
for(i=2;i<=a-1;i++)
{
c=a%i;
if(c==0)
{
return 1;
}
}
if(i==a)
{
return 0;
}
}
void main()
{
int n;
int ans;
cout<<"Enter number";
cin>>n;
if(isPrime(n))
{
cout<<"Number is prime";
}
else
{
cout<<"Number is not Prime";
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.