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...

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

  • 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

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

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

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

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

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