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
Question # 1
Karo-lina-s [1.5K]

Answer:dataset

Explanation:

data set because it is kida a set

7 0
3 years ago
Read 2 more answers
In a swap you need a variable so that one of the values is not lost ? Need help
emmasim [6.3K]

Answer:

In a swap, the variable is cuttly.x

Explanation:

7 0
3 years ago
Read 2 more answers
What do developers do to support software products? explain to users which development process model was used to make the produc
cestrela7 [59]

Answer:

The developer develops the user manuals to support their products. And agile development process is being used to make the user manual. The technical write this, but they first talk to developers, designers, project managers, team leaders etc. and collect all the information from them. And they start making them since start of the project. They use jira, git and various version control to update this document. And all the functionalities must be mentioned accurately, as well as latest of them to ensure reliability and credibility. Devops is another automated technology being used extensively these days.

Explanation:

The answer is self explanatory.

8 0
3 years ago
Consider the following recurrence, defined for n a power of 4 (for the time of some algorithm): T(n) = 3 if n = 1 2T(n/4) + 4n +
In-s [12.5K]

Answer:

Explanation: see attachment below

4 0
4 years ago
What is the basic body structure of html.
hodyreva [135]

Answer:

A line holding HTML version information, a declarative header section (separated by the HEAD element), and a body containing the document's actual content make up an HTML 4 page.

8 0
2 years ago
Read 2 more answers
Other questions:
  • If you delete an imessage does the other person see it
    12·1 answer
  • Which skill refers to the ability to visualize and implement possible business solutions to problems?
    6·1 answer
  • Indicate the proper order (1-4) of the following PR strategic planning 4-step process. 1 Defining the problem 2 Evaluating the p
    10·1 answer
  • Exchange is defined as the trade of goods between two nations. This phenomena is also known as ___________.
    9·1 answer
  • What was the original motivation for developing the internet?
    7·1 answer
  • What is the name of the process that weighs the alternatives, gathers all necessary information, and can ultimately lead you to
    9·2 answers
  • In which area of the screen can features and functions of Word be accessed?
    9·2 answers
  • Hannah decides to burn her video portfolio to a DVD. She wants to hand it over to a movie producer she will meet at a social eve
    15·1 answer
  • This is science I just couldn’t find it
    15·1 answer
  • What is the sequence of instructions performed to execute one program instruction
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!