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
13. A 2-sided coin has an equal likelihood of landing on each side. One side is called "heads" and the other is called "tails".
serious [3.7K]

Answer: c

Explanation:

only reasonable answer

7 0
2 years ago
1 byte =???? bit<br> please help me
diamong [38]

Answer:

8 bits make a byte

Explanation:

8 bits make a byte

7 0
2 years ago
A web designer must effectively communicate when he/she designs a web page. If there is too much information to put onto one pag
valina [46]
Usually they'll add a link to the bottom of the page to the next page.

7 0
2 years ago
In contrast to data in a database, data in a data warehouse is described as subject oriented, which means that it _____.
jonny [76]

Answer:

focuses on a certain subject

Explanation:

it means that it focuses on that one subject and none others

Hope it helps c:

6 0
2 years ago
What important advice to include about spyware, viruses and other types of malware
S_A_V [24]

Answer:

You should probably include the fact that most of these things will disguise themselves as fake downloads and that will most of the time claim to allow you to download something that costs money for free.

Explanation:

thats all i could come up with

5 0
2 years ago
Read 2 more answers
Other questions:
  • What is the outlined area called?
    6·1 answer
  • In a paragraph of no less than 125 words, explain what netiquette is and how it improves efficiency and productivity in the work
    14·1 answer
  • Populate a one-dimensional array with the following grades in this order: 90, 61, 74, 42, 83, 51, 71, 83, 98, 87, 94, 68, 44, an
    15·1 answer
  • Write the percentage 5 1/4 as a decimal​
    8·1 answer
  • Programmers insert documentation called facts into the program code.? <br> a. True <br> b. False
    9·1 answer
  • A user prefers an external monitor, mouse, and keyboard for a laptop. The user does not want to use the built-in screen; however
    5·1 answer
  • How does netbios identify a computer system on the network?
    13·1 answer
  • Project manager Erica is discussing with her team to prepare a document to describe the deliverables and goals of a software pro
    13·1 answer
  • 3.1.1 What type of goods are car radio and remote control.​
    12·1 answer
  • 5. The command to add new layout to the slide is present in<br>tab.​
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!