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
damaskus [11]
3 years ago
11

Write a program that prompts the user to enter in a postive number. Only accept positive numbers - if the user supplies a negati

ve number or zero you should re-prompt them. Next, determine if the given number is a prime number. A prime number is a number that has no positive divisors other than 1 and itself. For example, 5 is prime because the only numbers that evenly divide into 5 are 1 and 5. 6, however, is not prime because 1, 2, 3 and 6 are all divisors of 6.
Computers and Technology
1 answer:
marusya05 [52]3 years ago
5 0

Answer:

The c++ program to check prime numbers is shown below.

#include <iostream>

using namespace std;

int main() {

   int num, prime=0;

   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);

   

   if(num==1 || num==2 || num==3)

       cout<<num<<" is a prime number."<<endl;

   else if(num%2 == 0)

       cout<<num<<" is not a prime number."<<endl;

   else

   {

       for(int k=3; k<num/2; k++)

       {

           if(num%k == 0)

               prime++;

       }    

   if(prime>1)

       cout<<num<<" is not a prime number."<<endl;

   else

       cout<<num<<" is a prime number."<<endl;

   }

}

OUTPUT

Enter a positive number.

-7

Invalid number. Enter a positive number

0

Enter a positive number.

79

79 is a prime number.

Explanation:

The user input is validated for positivity. A do while loop along with an 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.

If user inputs 1, 2, or 3, message is displayed.

Else If user inputs an even number, message is displayed for not prime. 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.

For this, an integer variable prime 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 prime is increased by 1. If value of prime 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.

You might be interested in
1. Briefly explain the concept of signature of a function, in a function declaration. What signature components are crucial for
Irina18 [472]

Answer:

Function signature can be defined as a combined term used to refer to the function name, function return type, no of arguments , type of arguments.

Explanation:

The signature of function is seen as a combined term used to refer to the function name, function return type, number of arguments , type of arguments.

When overloaded functions is been defined, they are different in numbet of arguments or type of argument passed.

To understand this better refer to the program code below.

C++ code.

#include <iostream>

using namespace std;

int multiply(int a, int b)

{

cout << a*b <<endl;

return 0;

}

int multiply(int a, int b, int c)

{

cout << a*b*c <<endl;

return 0;

}

int main()

{

//function with two arguments passed

multiply(3, 50);

//function with three arguments passed . It is different in number of arguments passed. Thus here function signature is different

multiply(4, 20, 10);

}

6 0
2 years ago
What did major networks do to combat audience erosion in the 1990s?
S_A_V [24]

Answer: I think is 3. They acquired cable channels. They acquired cable operators.

Explanation:

6 0
3 years ago
What is contained in the Open Files section of Shared Folders? ​
amm1812

Answer:

The contents of shared folders might include both programs and data files. Common files may be stored and accessed using shared application folders, which simplify administration and offer a single area for users to store and access common data. If all data files are consolidated in a single shared folder, users will have an easier time finding them.

Explanation:

Hope it helps:)

8 0
2 years ago
What is Groovefunnels?
Andreyy89

GrooveFunnels is a game-changer and it’s disrupting the market quite a bit.
5 0
3 years ago
Read 2 more answers
The Securities and Exchange Commission (SEC) was created to develop and approve a set of common international accounting rules.
Dennis_Churaev [7]

Answer:  

Idk search it on google chrome

Explanation:

3 0
3 years ago
Other questions:
  • Katie created a web site that automatically scales to fit a cell phone. What kind of design did Katie use?
    12·2 answers
  • The LTE (cellular telephone) standard supports only packet switching"". What cellular services are morst affected by this change
    15·1 answer
  • Utilities software and word processing software are both eamples of
    10·1 answer
  • What is the maximum transmission speed for bluetooth v3 and v4 devices?
    12·1 answer
  • Can you please answer these questions for me: Is it illegal to copyright? What does it mean to get copyrighted? What are example
    12·2 answers
  • Going to Grad School! In the College of Computing and Software Engineering, we have an option for students to "FastTrack" their
    11·1 answer
  • How do you calculate typing speed
    8·1 answer
  • Use the drop-down menus to describe how Adnan can add shapes to his presentation.
    11·1 answer
  • 20 POINTS! Which music making software is better? Ableton Live or Logic Pro? Name the advantages and disadvantages of each one!
    7·2 answers
  • If a company saw an online photo of you playing basketball, it might try to sell
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!