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
dion training just installed a new webserver within a screened subnet. you have been asked to open up the port for secure web br
ryzh [129]

The port that should be set as open to allow users to access this new server is port 443.

<h3>What is a Computer port?</h3>

In regards to computer hardware, a port is known to be a tool that helps to act as a kind of an interface that tend to exist between the computer as well as other computers or peripheral units or tools.

Note that  the port connote  the aspect of a computing device that is made available for connection to other forms of  peripherals such as input.

Therefore, based on the work above, the port that should be set as open to allow users to access this new server is port 443.

Learn more about port from

brainly.com/question/28391396
#SPJ1

6 0
1 year ago
How can we compare a computer with human beings?
LenaWriter [7]

Answer:

Our brain as a central information processor vs. computer having a central processing unit

Our brain takes sensory input → processing/ sense making → in order to produce behavior as output. Computers work similarly: information input → processing → output task

Computers requires both hardware and software to function. Our body is essentially hardware, what we learn from formal education and informal life experiences are software.

Explanation:

8 0
4 years ago
5. Developed by Paul Hawkins, it is a computer system
il63 [147K]

Answer:

hawk eye

Explanation:

4 0
3 years ago
Plz help me! No guessing plz!
Tomtit [17]
C- World Wide Web - It has all web pages and files delivered by web servers 
4 0
4 years ago
A(n) Answer display color uses the least electricity when compared to any other color.
Elan Coil [88]

Is there answer choices because I’m not understanding what you want me to answer

3 0
3 years ago
Other questions:
  • Barr the Bear has started a business to sell fish to Poe and his fellow penguins. The penguin customers submit many fish orders,
    12·1 answer
  • You use this method to determine the number of items stored in an arraylist object.
    12·1 answer
  • Invention I chose was radio.
    9·1 answer
  • If you need help with a topic that isn't present in English or math labs, what should you do?
    13·2 answers
  • Retype the statements, correcting the syntax errors.
    9·1 answer
  • Garrett has a column of data. What can he do at the bottom of that column of data using an Auto function? Check all that apply.
    8·2 answers
  • Missing slot covers on a computer can cause?
    9·1 answer
  • Colin is a software developer. He would like to earn a credential that demonstrates to employers that he is well educated on sof
    15·1 answer
  • What is the advantage of processor affinity on SMP computers ?
    14·1 answer
  • In the 1760s and early 1770s, the British government wanted to raise money by taxing the residents of its colonies in North Amer
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!