Question

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 true, and false otherwise.

Input Validation
You can either assume the user will always input an integer value, or you can add input validation for the integer similar to the example shown in the midterm solution.
If the integer input is a negative number, output an error message.
Hints
Be sure and define the function prototype before main() calls it.
0 and 1 are not prime numbers.
2 is the only even number that is a prime number.
For numbers 3 on up, one way to determine whether the number is prime is to loop through all numbers from 2 through number - 1 to see if it is evenly divisible by any other number. Hint: use the modulus operator.
A more efficient way would be to check only the odd numbers from 3 through the square root of number since the square root represents the half way point for multipliers. This enhancement is not required, as long as your program produces the correct results.
Don't forget to add comments to explain what the code is doing and where control of the program is executing.
Choose variable names and function names that describe its purpose.
Sample Output
Please enter a number: 91

91 is not prime.
Please enter a number: 97

97 is prime. It is divisible only by 1 and itself.
Please enter a number: -1

-1 is not a positive integer.
Please enter a number: 0

0 is not prime.
Please enter a number: 1

1 is not prime.
Please enter a number: 2

2 is prime. It is divisible only by 1 and itself.

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

c++ code for given above statements:

#include <iostream>
using namespace std;
//boolean function to check prime number or not
bool isprime(int number)
{
//checking given number is prime or not
if (number <= 1)
{
return false;
}
// for loop upto given number range to check given number is prime or not
for (int i=2; i<number; i++)
{
//check numebr is divisible by for loop number
if ( number%i == 0)
{
return false;
}
}
return true;
}
int main()
{
int n;
cout<<"enter the positive integer number \n";
cin>>n;
// its check the given number is positive number or negative number
if (n < 0)
{
cout<<n<<" is not a positive integer. \n";
}
else
{
// function call and if its true is prime number else not prime number
isprime(n)? cout<<n<<" is prime. It is divisible only by 1 and itself. \n" : cout<<n<<" is not prime.";
}
return 0;
}

output:

case_1: n = 91

enter the positive integer number                                                                                                                             

91                                                                                                                                                            

91 is not prime.

case_2: n = 97

enter the positive integer number                                                                                                                           

97                                                                                                                                                          

97 is prime. It is divisible only by 1 and itself.

case_3 : n = -1

enter the positive integer number                                                                                                                           

-1                                                                                                                                                          

-1 is not a positive integer.

case_4: n = 0

enter the positive integer number                                                                                                                           

0                                                                                                                                                           

0 is not prime.

case_5: n = 1

enter the positive integer number                                                                                                                           

1                                                                                                                                                           

1 is not prime.

case_6: n= 2

enter the positive integer number                                                                                                                           

2                                                                                                                                                           

2 is prime. It is divisible only by 1 and itself

Note:

  • // indicates comment line for code line.
Add a comment
Know the answer?
Add Answer to:
Is Prime Number In this program, you will be using C++ programming constructs, such as functions....
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
  • In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a...

    In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter an integer value. Your program will then display that integer back to the user. Your program should include a function called getInteger that requests an integer value from the user and returns that value back to the caller. Your main () function will call the function getInteger and will then display the value returned by that function....

  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

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

  • 17. Prime Numbers A prime number is a number that is only evenly divisible by itself...

    17. Prime Numbers 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 S. The number 6, how- ever, is not prime because it can be divided evenly by 1, 2, 3, and 6. Write a Boolean function named is_prime which takes an integer as an argument and returns true if the argument is a prime number, or...

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

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

  • c++ write a program to calculate and store the first 10 prime numbers. Recall that a...

    c++ write a program to calculate and store the first 10 prime numbers. Recall that a prime number is divisible by 1 and itself. Function to use in program : isPrime

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

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

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