Question
Your program  write a program to test whether a number is prime or not.

Your program should ask user to input an integer and respond: "Prime" or "Not prime".

Don't use any library function that tests for prime numbers.

The program should continue to ask for input, till it sees a 0, when it exits.

Please submit printed pseudocodeshould ask user to input an integer and respond: "Prime" or "Not prime".

Don't use any library function that tests for prime numbers.

Thecopies of pseudocode, commented code

HOMEWORK write a program to test whether a number is prime or not. Your program should ask user to input an integer and respo
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudo code to check if the number n is prime or not.

if n is less than or equal to 1

n is not prime

else

for i=2 to i=square root of n

if i perfectly divides n //i. e n%i==0

n is not prime

end of loop.

if none of the number divides n, then n is prime.

here is the implementation of above code

#include <iostream>
using namespace std;
// function that return true if the a number is prime, otherwise return false
bool prime(int a){
if(a<=1)// not prime
return false;
for(int i=2;i*i<=a;i++)// we only need to check from i=2 to i=sqrt(n), if any number perfectly divides a, then a is not prime
{
if(a%i==0)// a is not prime
return false;
}
// if none of the number divides a, then a is prime, so return true;
return true;
}

int main() {
   int a;// To store the input given by the user
   while(1)
   {
   cin>>a;
   if(a==0)// if the given number is 0, loop breaks
   break;
   if(prime(a))
   {
   cout<<"Prime"<<endl;
   }
   else
   cout<<"Not Prime"<<endl;
   }
   return 0;
}
Please upvote if you liked the answer. Thank You.

Add a comment
Know the answer?
Add Answer to:
Your program  write a program to test whether a number is prime or not. Your program should...
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
  • Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime...

    Prime Numbers Write a program that will determine whether a set of randomly-generated numbers are prime numbers. To write your program, you will need to use the following programming features: The random library (module) A function A variable that will prompt the user to say how many random numbers the program should generate A for loop with a range function A randint function taken from the random library (module); the randint function takes two arguments – the beginning and the...

  • In this assignment you are asked to write a Python program to determine all the prime...

    In this assignment you are asked to write a Python program to determine all the prime numbers in between a range and store them in a list that will be printed when the range is searched. The user is prompted for two positive integer values as input, one is the start of the range and the other is the end of the range. If the user gives a negative value or enters a second number that is lower than the...

  • 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 a program to print all the prime numbers below a certain given number. A prime...

    Write a program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input and...

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • C++ Your goal is to ask the user for a number and to determine if that...

    C++ Your goal is to ask the user for a number and to determine if that number is a prime number. Use a function to determine if the number is prime. Your program should have the following:  The name of the program should be Assignment 6.  3 comment lines (description of the program, author, and date).  Ask the user for an integer. Store this result in a variable with an appropriate name. (1 point)  Write a...

  • A prime number is a number that can be evenly divided by only itself and

    A prime number is a number that can be evenly divided by only itself and 1. For example, the number 5 is prime because it can be evenly divided by only 1 and 5. The number 6, however isn't prime because it can be evenly divided by 1,2,3, and 6. Write a bool function named isprime that takes an integer as an argument and returns true if the argument is a prime number or false otherwise. Use the function in...

  • Write a multithreaded C program that outputs prime numbers. This program should work as follows: the...

    Write a multithreaded C program that outputs prime numbers. This program should work as follows: the user will run the program and will enter a number on the command line. The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user. ADD  "comments" to the source code please and a screenshot of the output with steps on how to compile

  • 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]:...

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