Question
I need help with this exercise. Thank you for your help
Language is C++
6. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd inte- ger is prime if it is not divisible by any odd integer less than or equal to the square root of the number.)

0 1
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;//Input from the user
  
   bool isPrime = true;//Let's assume the number is prime
  
for(int i = 2; i <= sqrt(n); i++)//Loop till square root of n
{
   if(n % i == 0)//If n is divisible by i then it is not prime
       isPrime = false;
   }
  
if(isPrime)//If n is prime
   cout << n << " is a prime number" << endl;
else
   cout << n << " is not a prime number" << endl;
  
   return 0;
}

OUTPUTS:
CAUsersiShubhamDesktop.Untitled1.exe Enter a positive integer 2 2 is a prime number Process exited after 1.255 seconds with return value e Press any key to continue . . . CAUsers Shubham Desktop Untitled1.exe Enter a positive integer 11 11 is a prime number Process exited after 0.7354 seconds with return value e Press any key to continue . .

Add a comment
Know the answer?
Add Answer to:
I need help with this exercise. Thank you for your help Language is C++ 6. Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to...

    write in C++ Exercise#1: Is it a Prime? Write a program that prompts the user to enter a positive integer in the range [100, 100000]. If the integer is not in the specified range the program prompts the user again. The program prints whether the integer is a prime number or not. Sample input/output nter an integer in the range [10e, 10eeee]: 39 nter an integer in the range [100, 100000]: 120453 Enter an integer in the range [10e, 10e000e]:...

  • Write code that prompts the user to input a number. The program should then output the...

    Write code that prompts the user to input a number. The program should then output the number and a message saying whether the number i "Positive, "Negative" or "Zero". It should further output whether the number is odd or even (zero is counted as even) For example:

  • use C++            Project 6 1. Using vectors or arrays, write a function named word_rev() that reverse...

    use C++            Project 6 1. Using vectors or arrays, write a function named word_rev() that reverse any given word. 2. Write a program that will: Ask the user to enter a word. Save the entry as word_entered. Call word_rev on the entry Display the reversed entry (word_entered) 3. Write a function named prime() that determine whether or not a given number n (greater than one) is prime. The algorithm: If n is even then n is not a prime number...

  • Exercise 9.2 Write a Python program that collects from the user a single integer. Have the...

    Exercise 9.2 Write a Python program that collects from the user a single integer. Have the program print “EVEN” if the integer is divisible by 2, and “ODD” if the integer is not divisible by two. [Use the % operator to compute the two’s modulus (remainder of division by two)] Exercise 9.3 Write a Python program that collects from the user two integers. Have the program print “Yes” if the two integers are BOTH greater than 10. Do nothing if...

  • Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...

    Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as "You win!" and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct...

  • Using the programming language C, read user input of an integer, check and output whether the...

    Using the programming language C, read user input of an integer, check and output whether the integer is an odd number or even number.

  • IN JAVA 1. Create a program that prompts the user for an integer and then displays...

    IN JAVA 1. Create a program that prompts the user for an integer and then displays a message indicating whether or not the number is a perfect square. This can be determined by finding the square root of a number, truncating it (by casting the double result), and then squaring that result 2. Write a program that prompts the user for two numbers. The first number is a minimum value and the second number is a maximum value. RandomNum then...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT