need help in c++ Write a function name isPrime, which takes an integer as an arg
ID: 3687685 • Letter: N
Question
need help in c++ Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the function in a complete program that reads in an integer that is less than 3001 stores a list of all the prime numbers from 2 through that number in a file named "PrimeList.txt" and to the standard output. requirements: -organize code within a function -pass data to and return Boolean data from function -Use of loop -Use file processing
Explanation / Answer
#include <iostream>
using namespace std;
bool isPrime (int num);
int main ()
{
int num=0;
cout << "Please enter a number and I will tell whether it is prime: ";
cin >> num;
if (isPrime(num)==true)
cout << num << " is prime.";
else
cout << num << " is NOT prime.";
return 0;
}
bool isPrime(int input)
{
if(input<1)
return false;
else if (input == 1||input ==2 ||input==3)
{
return true;
}
else
{
for(int i=2; i<input; i++)
{
if(input%i==0)
return false;
}
return true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.