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
How can people efficiently and effectively influence lots of people throughout the world
tatuchka [14]

Answer:

The word efficient denotes the ability to achieve a good goal toward a person, and effectiveness denotes reliable production or achievement.

Explanation:

Many people around the world can set efficient goals, that is, to influence or encourage other people to make good projects or changes in their lives, by example or simply teach them with patience and altruism.

In this way effectiveness will be seen, that is, the product or the achievement precisely when we observe how people undertake new projects in their lives

3 0
3 years ago
Stella is a bank executive. She is preparing a spreadsheet on the loan repayment schedules of customers. Which function can she
den301095 [7]

Answer: The function Stella can use to calculate the periodic payments of a loan is:

The Excel PMT function or NPER function.

Explanation: 1. The Excel PMT function is a financial function that returns the periodic payment for a loan.

2. The NPER function to figure out payments for a loan, given the loan amount, number of periods, and interest rate.

7 0
3 years ago
Which of the following is NOT an advantage of central management of information systems in the University System of Georgia? (A)
zzz [600]

Answer:

Independence

Explanation:

Management information system (MIS) is the system that acts as the backbone of an organization's activities, holding everything together. According to my research on MIS, I can say that based on the information provided within the question the one term that is not considered an advantage would be Independence. Since MIS doesn't really provide independence since the organization depends on the system.

I hope this answered your question. If you have any more questions feel free to ask away at Brainly.

6 0
3 years ago
4. Give name=
Svet_ta [14]

Answer:

What does Bob [1] return?

What about Bob[-2]?

What about Bob[1:-1]?

How to get the length of Bob?​

Explanation:

8 0
3 years ago
Eric is working on a computer that has a device driver error. Eric can find the name of the device driver however the actual dev
wel

Answer:

Windows\System32\drivers folder.

Explanation:

Eric operates on such a computer which has some failure on system's driver. Although the specific system may not be operational, he will locate the system driver title. Inappropriately he has no Connectivity to the internet.

Windows\System32\drivers directory could move to him to show the file of the system driver. Thus, the following answer is correct according to the given scenario.

3 0
3 years ago
Other questions:
  • #A year is considered a leap year if it abides by the #following rules: # # - Every 4th year IS a leap year, EXCEPT... # - Every
    5·1 answer
  • Why do you feel an organization has multiple layers of security in place to protect its operation?
    5·1 answer
  • Is a component that provides a button control in a gui application or applet?
    15·1 answer
  • What are the key ideas in dealing with a superior?
    9·1 answer
  • To reload a picture taken with a digital camera means to copy the digital picture from the camera to your computer.
    5·2 answers
  • Question 2Write a MIPS assembly implementation of the following C/C++ code. Assume small, unsigned integer arithmetic (no range
    10·1 answer
  • Fill in the blanks with the correct words.
    7·1 answer
  • Blockchain is often associated with Bitcoin and the financial services industry. However, it is applicable to almost every indus
    13·1 answer
  • Hisoka is creating a summary document for new employees about their options for different mobile devices. One part of his report
    7·1 answer
  • Steps to copy and paste text/information from website to Ms-word using keyboard​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!