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,...
Write following program using Python: A positive integer greater than 1 is said to be prime...
Write following program using Python: A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime. Write a program that asks the user to enter a integer greater than 1, then displays all of the prime numbers that are less than or equal to the number entered. Last, create two files. One file will hold all of...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
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.
A prime number is a number that is only evenly divisible by itself and 1. For...
A prime number is a number that is only evenly divisible by itself and 1. 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 evenly by 1, 2, 3, and 6. Design a Boolean function called isPrime, that accepts an integer as an argument and returns True if the argument is a prime number, or False otherwise. Use the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT