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
Which do switches create?<br> Networks<br> Wireless access points<br> Routes<br> Collision domains
REY [17]

Answer:

Network

Explanation:

As we know that switches create a network and routers connect networks. The two pieces of equipment look similar and perform some similar functions, but each has its own distinct function to perform on a network.

So, the first option <u>Netw</u><u>ork</u> is the correct answer.

6 0
3 years ago
Big Data often involves a form of distributed storage and processing using Hadoop and MapReduce.
olya-2409 [2.1K]

One reason for this is:  the processing power needed for the centralized model would overload a single computer.

<u>Explanation:</u>

Companies would be engrossed in achieving and examining the datasets because they can supplement significant value to the desicion making method. Such processing may include complicated workloads. Furthermore the difficulty is not simply to save and maintain the massive data, but also to investigate and extract a essential utilities from it.

Processing of Bigdata can consists of various operations depending on usage like culling, classification, indexing, highlighting, searching etc. MapReduce is a programming model used to process large dataset workloads. Hadoop is designed to scale up from single servers to thousands of machines, each offering local computation and storage.

5 0
3 years ago
How many miss Dragon Ball Abridged?
Troyanec [42]

What is "Dragon Ball Abridged"?

3 0
2 years ago
Read 2 more answers
Elise has just edited two photos of her cat and three different photos of her dog. She needs to save all five of these photos in
Lera25 [3.4K]
<span>The best answer is A. cat_1, cat_2, dog_1, dog_2, dog_3. Filenames are preferably short and should state the obvious. They should be unique and distinct, with no any duplicates in any given file within the same folder. The other choices either have duplicates, vague or are way too long to be identified quickly. </span>

<span> </span>

7 0
4 years ago
Read 2 more answers
Choose the term that matches the action.
AveGali [126]

Answer:

i guess patent thief might be the answer

5 0
3 years ago
Read 2 more answers
Other questions:
  • He would like to get rid of the graph. What will accomplish
    8·1 answer
  • If you want to present slides to fellow students or coworkers which productivity software should you use to create them A. Word
    14·2 answers
  • Short notes on Supply chain Management System (SCMS)
    12·1 answer
  • The Binder Prime Company wants to recognize the employee who sold the most of its products during a specified period. Write a qu
    12·1 answer
  • 11 Select the correct answer. Which external element groups items in a design?
    9·1 answer
  • Most of the energy we use originally came from:
    8·1 answer
  • 5. An external CSS file is saved with D. Answer the following questions. 1. Explain the purpose and advantages of using CSS.​
    7·1 answer
  • What are the local, state, and national opportunities that may be available to those who participate in CTSOs?
    9·1 answer
  • 2.Some artists look for ways to extend copyright for longer than the laws normally allow. Why do they do this?
    15·1 answer
  • What are the different elements of a window?​
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!