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
Oliver is creating an image of a fruit basket. From the basket, he wants the red apples to stand out among the rest so they will
Volgvan
He   can increase   the   thickness   of the paint on the   apples.
Or he can  use glossier  finish on the   apples  to make them stand out...
3 0
3 years ago
Bunch of points!!!!!!! help pls
alex41 [277]
Most likely B. Photographs.

Hope this helps!

Have a good day!
4 0
3 years ago
Read 2 more answers
What would the range(3, 9) function generate?
gregori [183]

Answer:

A, 3,6,9

is the answer

Explanation:

bc it count by

3 0
3 years ago
A scientific question may lead to a(n) , which can be tested.
kari74 [83]
A hypothesis or educated guess
6 0
3 years ago
Read 2 more answers
How do you return a value from a function?
rjkz [21]

Answer:

return instruction used to return a value from a function.

Explanation:

Function is a block of statement which perform the special task.

Syntax for define a function:

type name(parameter_1, parameter_2,...)

{

  statement;

return variable;

}

In the syntax, type define the return type of the function. It can be int, float, double and also array as well. Function can return the array as well.

return is the instruction which is used to return the value or can use as a termination of function.

For return the value, we can use variable name which store the value or use direct value.

4 0
3 years ago
Other questions:
  • which of the following is true about both the cyber community and the real community like the one you live in,i will give 10 poi
    6·1 answer
  • What is the main purpose of broadcasting via satellite? A. To enable signal reception at night B. To convert analog signals to d
    5·1 answer
  • There are several vehicles in a parking lot. Some of them are motorcycles
    8·2 answers
  • What were the goals of the “paperless society” ideal?
    14·2 answers
  • Example: An amount of $1,500.00 is deposited in a bank paying an annual interest rate of 4.3%, compounded quarterly. What is the
    5·1 answer
  • When you want to avoid sending email that a recipient may feel their privacy has been invaded, how would you fill in the (To) bo
    13·1 answer
  • A ___describes a particular attribute of the dates item in a table​
    14·1 answer
  • What area displays the title of the document
    14·1 answer
  • Write a program that takes the account's present value, monthly interest rate, and the number of months that the money will be l
    5·1 answer
  • Determine of population size​
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!