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

The number 2 is considered to be a prime number. One way to determine if an inte

ID: 641201 • Letter: T

Question

The number 2 is considered to be a prime number. One way to determine if an integer N>2 is a prime number is to check to see if it is evenly divisible by any number from 2 up to 1/2 of N. If a number is found that evenly divides N, then N is not prime, otherwise it is prime. Take for instance the number 25. We can check the values 2 through 12 to see if we find a divisor, as follows:
Is 25 divisible by 2? No
Is 25 divisible by 3? No
Is 25 divisible by 4? No
Is 25 divisible by 5? Yes
We can stop at this point, because we now have shown that 25 is not prime.
Now, let's consider the number 13. We can check the values 2 through 6:
Is 13 divisible by 2? No
Is 13 divisible by 3? No
Is 13 divisible by 4? No
Is 13 divisible by 5? No
Is 13 divisible by 6? No
We have now exhausted all possibilities, so we have shown that 13 is prime.
Your task for phase 1 is to write a program that does this. Your program should ask the user for a number N, then respond by displaying "Prime" or "Not prime".
Note that your program will have a loop, and that there are two reasons why you might leave that loop: 1) a number was found that evenly divides N, or 2) you have exhausted all possibilities.

Explanation / Answer

#include <iostream>

using namespace std;

int main(){
   int n;
   cout << "Enter a Number: ";
   cin >> n;
   int d = 2;
   int flag = 0;
   while(d <= n / 2){
       if(n % d == 0){
           flag = 1;
           break;
       }
       ++d;
   }
   if(flag){
       cout << n << " is not a prime Number" << endl;
   }
   else{
       cout << n << " is a prime Number" << endl;
   }
   return 0;
}

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