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
What internal hardware device does a computer use to connect to a wired or a wireless network?
IrinaVladis [17]

Answer:

it will be ethernet and wireless network adapter

Explanation:because, wireless card is will able to connect to network wirelessly ,ethernet card will connect to network with ethernet cable RG45

if you give more info about your question will; try to help you better.

Thanks

8 0
3 years ago
If you accidentally put an envelope in the Express Mail box will it still get to its destination?
Gnoma [55]
It depends because if you put a stamp on the envelope. But I think that it will still get to the destination. I think that it will either get sent to the destination or not. But you never know what is going to happen.
8 0
2 years ago
In the context of IT jobs in the information systems field. A ____ is responsible for database design and implementation
Yuri [45]

Answer: Database Administrator.

A Database Administrator is responsible for any design and implementation of a database in a system. They are also the people who make sure that they can secure and manage all the data in their system to avoid any problems that may occur during its usage.

They also are responsible for keeping track of changes and the accessibility of the users that will be inputting data into the database. This most especially applies to databases that are meant to keep client information and transactions for most companies.

8 0
2 years ago
Read 2 more answers
You created an outline within a worksheet. what does the button indicate to the left of a row heading?
cricket20 [7]
<span>Business Name (default field), State (correct field)</span>
3 0
2 years ago
Game Informer (often abbreviated to GI) is an American-based monthly magazine featuring articles, news, strategy, and reviews of
Eddi Din [679]

Answer:

it is d i just took it

Explanation:

6 0
3 years ago
Read 2 more answers
Other questions:
  • Consider a file system that uses inodes to represent files. Disk blocks are 2KB in size and a pointer to a disk block requires 4
    13·1 answer
  • Ask the user to enter a number n &gt;= 1. There after the user will enter n-1 distinct integers between 1 and n. Thus, the enter
    9·1 answer
  • Meg is in the process of creating a storyboard for her personal website, but she is unable to decide which storyboarding techniq
    10·1 answer
  • Write a function to reverse a given string. The parameter to the function is a string. Function should store the reverse of the
    12·1 answer
  • The word Only is absolute or qualified
    8·2 answers
  • Applications software is also known as
    14·2 answers
  • 1. Briefly explain the concept of signature of a function, in a function declaration. What signature components are crucial for
    9·1 answer
  • I dont understand this at all! PLZ help me
    14·1 answer
  • I need help!!!! 1.04
    9·1 answer
  • 14 Copy a picture of a crane on the next page. Do not trace them. Make a freehand sketch. (2) 2 Look at the placed where the Mar
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!