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

c++ A prime number is an integer greater than 1 that is evenly divisible by only

ID: 675033 • 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).

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();
}

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