Question

In: Computer Science

Create another program that inputs an integer from a user into a variable named: userInput Check...

Create another program that inputs an integer from a user into a variable named: userInput

Check to see if the number is a prime number. If it is, the program should display:

xx is a Prime Number

-or-

xx is NOT a Prime Number

(where xx is the value in userInput

Solutions

Expert Solution

Hi,

Please find the below code in CPP with description:

#include <iostream>
using namespace std;

int main() {
    //Declare variables
    int i, userInput;
    
    //Declare boolean Variable
    bool isPrime = true;
    
    //Show Message tot end user and Get Input from user
    cout << "Enter a positive integer: ";
    cin >> userInput;

    // 0 and 1 are not prime numbers
    if (userInput == 0 || userInput == 1) {
        //Set boolean value to false if number are 0 or 1 entered by user without checking any calculations
        isPrime = false;
    }
    else {
        //Number for other than 0 or 1, we need to do calculations to check number is prime or NOT
        //loop start from 2
        //first try dividing it by 2, and see if you get a whole number
        for (i = 2; i <= userInput / 2; ++i) {
            if (userInput % i == 0) {
                //If you don't get a whole number, next try dividing it by prime numbers: 3, 5, 7, 11 
                //(9 is divisible by 3) and so on
                //if number is divisibly by 2 then its not a prime number
                isPrime = false;
                break;
            }
        }
    }
    
    //here check the boolena flag and accordinly print the message
    if (isPrime)
        cout << userInput << " is a prime number";
    else
        cout << userInput << " is NOT a prime number";

    return 0;
}

In the above example, 55 is divisible by 5 and 11 so 55 is NOT prime number

In case of 71, this number is not divisible by any number so 71 is prime number.

Thanks.


Related Solutions

Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
Create a random number generator object named myRandom and an integer variable named intRoulette. Set intRoulette...
Create a random number generator object named myRandom and an integer variable named intRoulette. Set intRoulette to be a random number from 0 to 36 (including the numbers 0 and 36). (visual studios 2015) using tryparse
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to...
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method uses 2 ref parameters: feet, inches left of type int and a parameter that is not ref of type int to which you pass inchesinches. For example, 67 inches is 5 feet 7 inches.
Write a C++ code for these 2 questions 1. Create an integer variable named ‘arraySize’ and...
Write a C++ code for these 2 questions 1. Create an integer variable named ‘arraySize’ and initialize the value to 1000. 2.. Open an output file named ‘GroupAssignment2Results.csv’. The first line of this file should be the column headings: ‘Array Size’, ‘Bubble Sort Time’, ‘Selection Sort Time’, ’Insertion Sort Time’, ‘Quick Sort Time’, ‘Merge Sort Time’.
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT