1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
natta225 [31]
3 years ago
11

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 be

cause it can only be evenly divided 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. Tip: Recall that the % operator divides one number by another and returns the remainder of the division. In an expression such as num1 % num2, the % operator will return 0 if num1 is evenly divisible by num2.
Computers and Technology
1 answer:
Gemiola [76]3 years ago
4 0

Answer:

The c++ program to check prime numbers is shown below.

#include <iostream>

using namespace std;

bool isPrime(int n);

bool isPrime(int n)

{

   bool prime;

   int p=0;

   

   if(n==2 || n==3)

       prime = true;

   else if(n%2 == 0)

       prime = false;

   else

   {

       for(int k=3; k<n/2; k++)

       {

           if(n%k == 0)

               p++;

       }

   

   if(p>1)

       prime = false;

   else

       prime = true;

   }

   

   return prime;

}

int main() {

   int num;

   do

   {

       cout<<"Enter a positive number."<<endl;

       cin>>num;

       if(num<1)

       {

           cout<<"Invalid number. Enter a positive number"<<endl;

           cin>>num;

       }

   }while(num<2);

   

   cout<<"The "<<num<<" is prime. "<<isPrime(num)<<endl;

   

   

}

 

OUTPUT

Enter a positive number.

-4

Invalid number. Enter a positive number

0

Enter a positive number.

101

The 101 is prime. 1

Explanation:

The user input is validated for positivity. A do while loop along with if statement is implemented for verification.

do

   {

       cout<<"Enter a positive number."<<endl;

       cin>>num;

       if(num<1)

       {

           cout<<"Invalid number. Enter a positive number"<<endl;

           cin>>num;

       }

   }while(num<1);

The test for prime number is done by using multiple if else statements and a Boolean variable prime is used.

If user inputs 2 or 3, variable prime is set to true.

Else If user inputs an even number, variable prime is set to false. This is done by taking modulo of the number upon division by 2.

Else if user inputs neither an even number nor a number less than 3, the modulus of the number is taken with divisors beginning from 3 up to half of the input number. Here, an integer variable p is used and based on its value, variable prime is set to true or false.

For this, an integer variable p is initialized to 0. A number can be completely divisible by itself or by its factors.

If the number is divisible by any of the divisors, value of variable p is increased by 1. If value of p is greater than 1, this means that the user input is divisible by more than one divisor. Hence, the given number is not a prime number and the variable prime is set to false. Otherwise prime will be set to true.

The value 1 indicates true and 0 indicates false.

You might be interested in
You can use tables for layout work. However, in XHTML, each form control should have its own ______ element
777dan777 [17]

Answer:

c. label

Explanation:

4 0
3 years ago
For all those among our fans and SpongeBob fans which is better SpongeBob or amount us
Ivenika [448]

Answer:

!!!!SPONGEBOB!!! XD

Explanation:

5 0
3 years ago
Read 2 more answers
Assume you are a manager in the security department of a high-tech corporation. You are mentoring Mary, an entry-level network t
nadya68 [22]

Answer:

Seek a bachelor's degree.

Explanation:

Seems more compromised and education from people with experience already.

6 0
3 years ago
What happens if you give false information on your driver license application?
Artist 52 [7]

Answer:

<em>Cancelled</em>

Explanation:

Providing false information on your driver license application will result in your license being cancelled.

Giving the DMV any false info is considered a crime.

Hope this helped!

6 0
2 years ago
Given a string variable named sentence that has been initialized , write an expression whose value is the the very last characte
Blizzard [7]
Strings can usually be dealt with as arrays of characters.

sentence[ -1 ]

sentence.substring( sentence.size() -1 );
8 0
3 years ago
Other questions:
  • A model release can be either oral or written down. true or false
    11·2 answers
  • The two ways to use the help menu is by searching of the contents or searching the _____
    13·2 answers
  • Which of the following explains the growing need for a standard IT professional licensing program? Check all of the boxes that a
    11·1 answer
  • Most Internet users access commercial websites, which have higher-quality information because of higher editing standards and th
    12·1 answer
  • Write a program that takes the radius of a sphere (a floating-point number) as input and then outputs the sphere’s: Diameter (2
    7·1 answer
  • Which type of financial institution typically has membership requirements?
    14·1 answer
  • Consider the program below: public class Test { public static void main( String[] args ) { int[] a; a = new int[ 10 ]; for ( int
    5·1 answer
  • Speed(?)
    5·1 answer
  • PLEASE I NEED HELP WITH THIS, IS FOR TODAY
    10·2 answers
  • What does data warehousing allow organizations to achieve?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!