Question

In: Computer Science

C++ Create a program that checks whether a number is a prime number and displays its...

C++

Create a program that checks whether a number is a prime number and displays its factors if it is not a prime number.

Console

Prime Number Checker

Please enter an integer between 1 and 5000: 5

5 is a prime number.

Try again? (y/n): y

Please enter an integer between 1 and 5000: 6

6 is NOT a prime number.

It has 4 factors: 1 2 3 6

Try again? (y/n): y

Please enter an integer between 1 and 5000: 200

200 is NOT a prime number.

It has 12 factors: 1 2 4 5 8 10 20 25 40 50 100 200

Try again? (y/n): n

Bye!

Specifications

  • A prime number is divisible by two factors (1 and itself). For example, 7 is a prime number because it is only divisible by 1 and 7.
  • Assume that the user will enter a valid integer.
  • If the user enters an integer that’s not between 1 and 5000, the program should display an error message.
  • If the number is a prime number, the program should display a message.
  • If the number is not a prime number, the program should display a message. Then, it should display the number of factors for the number and a list of those factors.
  • Store the factors for each number in a vector.

Solutions

Expert Solution

#include <iostream>
using namespace std;

int isPrime(int n)
{
    for (int i = 2; i < n; i++) {
        if (n % i == 0) {
            return 0;
        }
    }
    return 1;
}


int main(){
   int n;
   char ch = 'y';
   
   while(ch=='y'){
      cout<<"Please enter an integer between 1 and 5000: ";
      cin>>n;
      
      if(isPrime(n)){
             cout<<n<<" is a prime number."<<endl;
      }
      else{
          int count = 0;
          for(int i = 1;i<=n;i++){
              if(n%i==0){
                  count++;
              }
          }
          cout<<"It has "<<count<<" factors: ";
          for(int i = 1;i<=n;i++){
              if(n%i==0){
                  cout<<i<<" ";
              }
          }
          cout<<endl;
             cout<<n<<" is NOT a prime number."<<endl;
      }
      cout<<"Try again? (y/n): ";
      cin>>ch;
      //cin>>ch;
   }
   cout<<"Bye!";
   return 0;
}

Related Solutions

Create an application that checks whether an integer is an odd or even number. Welcome to...
Create an application that checks whether an integer is an odd or even number. Welcome to the Odd/Even Checker! Enter an integer: ten Error! Invalid integer. Try again. Enter an integer: 10.3 Error! Invalid integer. Try again. Enter an integer: 10 The number 10 is even. Continue? (y/n): Error! This entry is required. Try again. Continue? (y/n): y Enter an integer: 9 The number 9 is odd. Continue? (y/n): n Specifications: Create a version of the Console class presented in...
In C, create a program that displays the minimum and maximum values stored in a data...
In C, create a program that displays the minimum and maximum values stored in a data file "datafile.txt". Use the following function prototype:  void minmaxarray(float value, float *min, float *max);
In C# Create a windows application which accepts the month number and displays the month name...
In C# Create a windows application which accepts the month number and displays the month name in a label.   Use a nested if... else statement to determine the month name. For months not in the range 1-12 display the message "Not a valid month"
1- Create a Java program to determine a certain number whether that number is odd or...
1- Create a Java program to determine a certain number whether that number is odd or even. Hint : use arithmetic operators and expressions to find the odd or even numbers. 2- Create a Java program to ask a user to enter a name and password as shown below: name is “Ahmed” and his password is 2321 or name is “Ali” and his password is 6776 . The program shows a greeting “Hi ..Welcome to my program” if the user...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending...
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending order. Use a stack to store the prime numbers.
Java program Prime Numbers A prime number is a natural number which has exactly two distinct...
Java program Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a java program which reads a list of N integers and prints the number of prime numbers in the list. Input: The first line contains an integer N, the number of elements in the list. N numbers are given in the following lines. Output:...
c++ A program that displays the status of an order. a) Program uses 2 functions (in...
c++ A program that displays the status of an order. a) Program uses 2 functions (in addition to main ()). b) The first function asks the user for the data below and stores the input values in reference parameters. c) Input data from user: # of spools ordered, # of spools in stock, any special shipping & handling charges over and above the $10 rate. d) The second function receives as arguments any values needed to compute and display the...
In this exercise, you will create a program that displays the amount of a cable bill....
In this exercise, you will create a program that displays the amount of a cable bill. The amount is based on the type of customer, as shown in Figure 10-30. For a residential cus- tomer, the user will need to enter the number of premium channels only. For a business customer, the user will need to enter the number of connections and the number of premium channels. Use a separate void function for each customer type. If necessary, create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT