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

Write a program in C++ to check whether a number is prime or not
Write a program in C++ to check whether a number is prime or not
Write a C program that creates a structure and displays its content. • Create a struct...
Write a C program that creates a structure and displays its content. • Create a struct that will be used to hold a student's name, age, and year in school (Freshman, Sophomore, Junior, or Senior) • Within function main, dynamically allocate space to hold the structure and assign a pointer to point to the memory space allocated • Read in (from the keyboard) the student's name, age, and year in school • Create a separate function with the prototype: void...
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);
C programming Get a number from the user and write a program that checks to see...
C programming Get a number from the user and write a program that checks to see if the inputted number is equal to x*x+x and x must be greater than 0. For example, if the user inputs 12. The program should check to see if 12 = x*x+x. In this case it is true because 3*3+3 = 12.
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...
Write a program that checks whether or not a date entered in by the user is...
Write a program that checks whether or not a date entered in by the user is valid. Display the date and say if it is valid. If it is not valid explain why. The input may be in the format mm/dd/yyyy, m/d/yyyy, mm/d/yyyy, or m/dd/yyyy. A valid month (mm) must be from 1 to 12 A valid day (dd) must be from 1 to the appropriate number of days in the month April, June, September, and November have 30 days...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
create a c++ proram to accept a positive number in the main program. Create and call...
create a c++ proram to accept a positive number in the main program. Create and call the following functions 1. 1. OddorEven -to determine if the number is an odd or even number ( do not use $ or modul opeartor) and will return " even number " or " odd number ". 2. Factorial - to compute the factorial of N. 3. Power - to compute the power of N without using pow function. display the result in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT