Question

In: Computer Science

A prime number is an integer greater than 1 that is evenlydivisible by only 1...

A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided by 1, 2, 3, and 6.

Write a Boolean function named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, and false otherwise. Demonstrate the function in a complete program.

Your program must have at least 3 user-defined functions is as follows:

  • get_input function

  • isPrime function

  • main function

Here's an algorithm to solve this problem:

// main function

do

{

// call get_input function to input a positive integer (whole numbers > 0)

// call isPrime function.

     // send the positive integer returned from get_input as an argument to isPrime.

// if the value returned from isPrime is true,

   // display that the input data (display the actual value of the number) is a prime number;

// else

   // display that the input data (display the actual value of the number) is not a prime number;

// prompt user to input an answer whether he/she wants to go through the above process with

// another positive integer.

// get the user’s answer.

} while (answer == ‘y’ || answer == ‘Y’)

// get_input function

// prompt the user to input an integer.

// do data validation on the entered integer;

// that is, set up a loop, and as long as the integer is not

// positive, have the user enter a positive integer.

//   once a positive integer is entered, that integer needs to be returned back to the main

// function.

// isPrime function

// this function receives a positive integer as a parameter.

// this function returns a value of true or false back to main.


in C++  

Solutions

Expert Solution

ANSWER:-

#include
using namespace std;
int get_input()
{
   int n;
   do
   {
       cout<<"enter a number : ";
       cin>>n;
   }while(n<=0);
return n;
}
bool isPrime(int n)
{
   if (n <= 1)
return false;
  
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;

return true;
}
int main() {
   int n;
   char answer;
   bool res;
   do
   {
       n=get_input();
       res=isPrime(n);
       if(res)
       cout<<"the number "<        else
       cout<<"the number "<        cout<<"if you want above process again enter y/Y : ";
       cin>>answer;
   }while (answer == 'y' || answer == 'Y');
   return 0;
}

OUTPUT:-

enter a number : 5
the number 5 is prime 
if you want above process again enter y/Y : y
enter a number : 6
the number 6 is not prime 
if you want above process again enter y/Y : n

Related Solutions

A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. Java
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. b) Modify the...
A prime number (or prime) is a natural number greater than 1 that has no posítive...
A prime number (or prime) is a natural number greater than 1 that has no posítive divisors other than 1 and itself. Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime...
Prove by strong mathematical induction that any integer greater than 1 is divisible by a prime number.
Python question Recall that a prime number is an integer that is only divisible by 1...
Python question Recall that a prime number is an integer that is only divisible by 1 and itself. For example, numbers 2, 3, 5, 7, 13, 19 are prime, whereas 4, 10, 12, 100 are not. Also, recall that factors are the numbers you multiply to get another number. For example, 24 has 8 factors: 1, 2, 3, 4, 6, 8, 12, and 24. As you know, any number can be factorized into several (possibly repeating) prime factors. For instance,...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5),...
Prove the statement by using definitions and assumptions only: if an integer greater than 4 is...
Prove the statement by using definitions and assumptions only: if an integer greater than 4 is a perfect square then the immediately preceding integer is not prime.
Let p be an integer other than 0, ±1. (a) Prove that p is prime if...
Let p be an integer other than 0, ±1. (a) Prove that p is prime if and only if it has the property that whenever r and s are integers such that p = rs, then either r = ±1 or s = ±1. (b) Prove that p is prime if and only if it has the property that whenever b and c are integers such that p | bc, then either p | b or p | c.
1) How many positive integers are greater than 140, less than 30800 and relatively prime to...
1) How many positive integers are greater than 140, less than 30800 and relatively prime to 280.
A number is prime if it can be divided exactly only by 1 and itself. Here...
A number is prime if it can be divided exactly only by 1 and itself. Here is an algorithm for checking if an input number is prime: function out = isprime(m)             for j:=2 to m-1             if mod(m,j) ==0 then             return “input is not a prime”             endif             endfor             return ”input is a prime” Let n denote the size of the input m, i.e. the number of bits in the binary expression for m. What is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT