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 buy a $3,500 car and finance it through the car dealer. the contract says if you are two months delinquent with your payment
photoshop1234 [79]
The car owner acquired his car because the car dealer offers him a financing credit services. It is part of the contract that the car owner should pay in monthly basis. It is a fact and part of the contract also that once the car owner failed to pay 2 months of his contribution, the car dealer will get his car back.
6 0
4 years ago
Does anyone know? If you do please answer.
Sidana [21]
Whats the question..
6 0
3 years ago
Why do we use modem while<br>accessing the internet or email​
Dafna1 [17]

Answer: Modems: Your gateway to the internet

To bring the internet into your home, you're going to need a modem. This is a small device that connects to your internet service provider (ISP) to tap into all that internet goodness. ... Your modem shares this connection with a computer or a router via an Ethernet cable.

Explanation:

7 0
4 years ago
Element primer a partir del qual es generarà l'energia central solar fotovoltaica
Juliette [100K]
No speak a Spanish ............
7 0
3 years ago
What is the difference between operating systems and application software?
mojhsa [17]

Answer:

The main difference between operating system and application software is that an operating system is a system software that works as the interface between the user and the hardware while the application software is a program that performs a specific task.

Explanation:

5 0
3 years ago
Other questions:
  • Define a function print total inches, with parameters num_feet and num_inches, that prints the total number of inches. Note: The
    12·1 answer
  • You're trying to decide which disk technology to use on your new server. the server will be in heavy use around the clock every
    10·1 answer
  • An administrator working on a Windows Server 2016 Server Core installation needs to disable DHCP on all interfaces on the server
    12·1 answer
  • Give two reasons to break up
    11·2 answers
  • My PC won't output any data does anyone have any ideas​
    9·1 answer
  • Puter Science (IS)
    14·1 answer
  • Create a program to determine the largest and smallest number out of 15 numbers entered (numbers entered one at a time). This sh
    11·1 answer
  • Which are guidlines for using themes? Check all that apply
    13·1 answer
  • Convert Octal 623, to Decimal number ​
    10·1 answer
  • PYTHON CODING QUESTION
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!