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
When adding clip art to a slide you are limited to the pictures stored on your computer?
77julia77 [94]
The answer is No.  <span>When adding clip art to a slide you are not limited to the pictures stored on your computer.  </span><span>We are not limited to using clip art from our computer. Any clip art can be used, but if it is from another source, it must first be saved to your computer as a </span>file<span>. </span>
8 0
3 years ago
There are___standard colors for text in a theme.
Stella [2.4K]
There are two standard colors for a text in a them
6 0
3 years ago
What conversion factor should be used to convert from meters to Gigameters?
hodyreva [135]
Meters * 1,000,000,000 = gigameters
5 0
2 years ago
How does a Graphical User Interface (GUI) interact with a desktop or laptop computer?
Viefleur [7K]

Windows, icons, menus, and pointers does  a  graphical  user interface (GUI) interact with a  desktop or laptop computer.

  • Windows, icons, menus, and pointers

<u>Explanation:</u>

Graphics user interface (gui) made a big resolution on desktop or laptop or tablet or workstation industries. In olden days till  1994 still, people were using the black and white computer where a desktop consists of keyboard and printer and monitors where display color white and black.

If we open a picture it will display only in black and white so games are in black and white mode. After windows  3.1  we have seen color picture and mouse interface is used. Since technology developed and interface in GUI is also developed improved in windows icon menu and mouse pointer.

As technology developed we going back to a dark mode such as black and white mode.

4 0
3 years ago
The Matlab Script should:1. Clear the command window2. Clear the workspaceQuestion 1: Why do we clear the command window and wor
finlep [7]

Answer:

This is done for the simple reason of having more space to work on

Explanation:

This is done for the simple reason of having more space to work on. By clearing the command window and workspace you provide yourself with sufficient space to create new commands without the clutter of the previous commands. This also prevents your focus from shifting towards old commands and allows you to simply focus on the commands you are currently working on. This does not clear all variables from the script, it only clears the current screen but the previous commands can still be accessed by using the up-arrow key

5 0
2 years ago
Other questions:
  • Machine language library routines are installed on computers Select one: a. because they can come as part of the operating syste
    15·1 answer
  • Rachel is on her way to an interview for the position of a project manager. She is trying to prepare for this interview by analy
    6·1 answer
  • The _____ is a narrative description of the product, service, or information system.
    7·1 answer
  • can An intelligent workplace uses technology to allow workers to be productive whether they are in the office or working from ho
    12·1 answer
  • A(n) _____ bus enables the central processing unit (CPU) to communicate with a system’s primary storage.
    5·1 answer
  • Super easy question but you have to think about it because it’s not that easy I’ll mark brainliest for first answer Explain the
    11·1 answer
  • Terminal emulation, especially the unprotected ____________________ protocol, should be blocked from any access to all internal
    9·1 answer
  • Https://www.blooket.com/play?id=300932<br> please
    8·2 answers
  • Help me asap please
    15·1 answer
  • What was the original name of the Dream SMP?
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!