Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prime numbers are number which are only divisible by themselves and the number 1

ID: 3536380 • Letter: P

Question

Prime numbers are number which are only divisible by themselves and the number 1. Example: 5 is a prime number because it can only be divided by 1 and 5. 6 is not a prime number because it can be divided by 1, 2, 3, and 6.

Write a function named isPrime which takes an integer as an argument and returns true if the argument is a prime number, or false if it is not.

Once you have your is prime method, prompt the user for an integer and then list all of the prime numbers from 2 up to and including the integer the user entered (if that integer is also prime).

Explanation / Answer

#include<iostream>

using namespace std;




void generate(int n)

{

if(n<2)

return;


cout<<2<<endl;

if(n==2)

return;


bool isPrime;

for(int i=3;i<=n;i=i+2)

{

isPrime=true;


for(int j=2;j<= i/2;j++)

if(i%j==0)

isPrime=false;


if(isPrime)

cout<<i<<endl;

}




}




int main()

{

int n;

cout<<"Enter the number : ";

cin>>n;

generate(n);


cout<<endl;

system("pause");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote