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
What type of impacts can a computer virus have on a computer?
kolbaska11 [484]
Viral dna can be detected by PCR .
5 0
3 years ago
"It's inventiveness ,uncertainty and futuristic ideas,typically deals with science and technology,,what is it called??
Novosadov [1.4K]

<em>Answer:</em>

<em>It is because inventiveness is risky. It disrupt the established social contract, in the same way that invasive species do in natural ecosystems. Inventors best practice to protect themselves from angry response is to include in its marketing campaigns, the costly credibility of science support, the power of technology, the propaganda machine of the government, and the imagination of those in the science-fiction writing business. With a few bucks well spent —the most ridiculous, unnecessary and toxic invention could be a success in the marketplace. </em>

5 0
3 years ago
Complete the following tasks. When you are finished, click Done to close the lab and move to the next question. After you click
DiKsa [7]

What would be the correct cable would be an DSL cord for the phonelines.

8 0
3 years ago
What helps in determining the reliability of a person to repay debt?
faust18 [17]

C. The amount of credit score

3 0
3 years ago
Read 2 more answers
True or false: when considering data backups, it is most important to verify that the integrity of the backup file or data is va
Alika [10]

your correct answer is true

5 0
3 years ago
Other questions:
  • Gina is upgrading your computer with a new processor. She installs the processor into your motherboard and adds the cooling syst
    5·2 answers
  • Operating systems such as Microsoft Windows use a specific character before a file extension to separate it from the filename. W
    9·2 answers
  • Which of these shortcuts becomes available only when text is highlighted?
    10·1 answer
  • Which event most led to a reduction in the size and cost of computers? a. invention of the Bessemer converter b. development of
    8·2 answers
  • Create 3–5 questions about your data that can be answered by sorting and filtering the database.
    8·1 answer
  • What things have small motors
    13·1 answer
  • For as long as you can remember, you've never been able to get a taco on Tuesday at your local taco truck. The lines are always
    8·1 answer
  • See the lseek_example.c file. Modify the lseek_example.c, such that it reads from an input file (named "start.txt") and will pri
    6·1 answer
  • Which statement creates a StudentIDs object given the following code: public class StudentIDs&gt; { private TheType item1; priva
    15·1 answer
  • Which element of the MakeCode Arcade interface represents an individual
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!