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
9. Alcohol begins to affect your brain
shusha [124]

Answer:

m

Explanation:

4 0
3 years ago
Read 2 more answers
Write a JavaScript program to generate the following pattern but the number of rows should be user input.
Nookie1986 [14]

Answer:

Testicles

Explanation:

u sukkk

3 0
3 years ago
Congress are smart becuase.....
spin [16.1K]

Answer:

If we are talking about government then they are smart bc of there two house policies, both are equal yet super unique. They make sure each state has an equal voice in the senate.

5 0
3 years ago
List two functions of the UPS​
Alja [10]

Answer:

To protect equipment in the event of power-cut

To ensure stable supply of current (protects against surges)

Explanation:

4 0
3 years ago
A for loop is most useful for: A. executing some statements for a specific number of iterations B. executing some statements for
dsp73

Answer:

Option A and C

Explanation:

  • Loops are control structures used to rehash a given area of code a specific number of times or until a specific condition is met. Visual Basic has three principle sorts of circles: for..next circles, do circles and keeping in mind that circles.  
  • For loop is a programming language contingent iterative proclamation which is utilized to check for specific conditions and afterward over and over execute a square of code as long as those conditions are met.
4 0
3 years ago
Other questions:
  • Hydropower uses moving water to do work, such as grinding grains in a mill. True False
    7·1 answer
  • _____ is the standard that enables wireless devices to access web-based information and services. world wide web tcp/ip ethernet
    5·1 answer
  • Regularly Tuning up a computer can assist keeping it running at Peak speed. True or False?
    12·1 answer
  • What is a credit card reader usually paired up with in order to take a credit card transaction?
    11·1 answer
  • Assuming that a year has 365 days, write a class named DayOfYear that takes an integer representing a day of the year and transl
    6·1 answer
  • The Printer Event 1 P1 requests and is allocated the printer R1. 2 P1 releases the printer R1. 3 P2 requests and is allocated th
    10·1 answer
  • Write a program that includes functions to calculate feet to inches, inches to feet, feet to yards, and yards to feet. Then, acc
    9·2 answers
  • The term used to describe the shape and layout of a computer component such as a motherboard or hard drive is ______ factor.
    5·1 answer
  • What is subscriber billing in Google Play and where does the money go?
    5·1 answer
  • For what reason can security risks never be fully eliminated?​
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!