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 or not
variable_2: a numeric value indicating either the first divisor found (if the number is not prime) or the number of divisors tried (if the number is prime)
Note that the main function needs to print either of two rather different looking output messages depending on the results returned from the isPrime function.
C++ Function:
/* C++ Program that checks whether given number is prime or not */
#include <iostream>
using namespace std;
//Function that checks for prime number
bool isPrime(int n, int *res)
{
int k;
//Iterating from 2 to 1 less than the
number
for(k = 2; k <= n - 1; k++)
{
//If there is any
divisibility, it is not a prime number
if ( n % k == 0 )
{
//Storing first divisor found
*res = k;
//Return false
return false;
}
}
//If k is equal to n, it is a prime
number
if ( k == n )
{
//Updating result
value
*res = n-2;
//Return true
return true;
}
}
//Main function
int main()
{
int n, res;
bool prime;
//Iterate till user enters a positive
integer
do
{
//Reading a positive
integer
cout << "\n\n
Enter a positive integer: ";
cin >> n;
}while(n <= 0);
//Calling function
prime = isPrime(n, &res);
//If given number is prime number
if(prime)
{
//Printing message
cout << "\n\n
Given number is a prime number... \n\n Total number of divisors
tried: " << res << " \n";
}
else
{
//Printing message
cout << "\n\n
Given number is not a prime number... \n\n First divisor found: "
<< res << " \n";
}
return 0;
}
______________________________________________________________________________________________
Sample Output:
#include <iostream> using namespace std; bool check_prime(int); int main() { int n; cout << "Enter a positive integer: "; cin >> n; if (check_prime(n)) cout << n << " is a prime number."; else cout << n << " is not a prime number."; return 0; } bool check_prime(int n) { bool is_prime = true; // 0 and 1 are not prime numbers if (n == 0 || n == 1) { is_prime = false; } for (int i = 2; i <= n / 2; ++i) { if (n % i == 0) { is_prime = false; break; } } return is_prime; }
A positive integer is a prime number if its only positive integer divisors are itself and...
A prime number is a positive integer whose positive integer divisors are 1 and the number. E.g. 17 is a prime number -23 is a not prime number 28 is not a prime number -45 is not a prime number The only even prime number is 2 Write a boolean function, isPrime that return true if its integer parameter is a prime and false if its integer parameter is not a prime number. A simple prime number algorithm is if...
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 defines two functions is_prime() and list_primes(). is_prime() will determine if a number is prime. list_primes() will list all the prime numbers below itself other than 1. For example, the first five prime numbers are: 2, 3, 5, 7 and 11." THE PROGRAM...
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...
A Prime Number is an integer which is greater than one, and whose only factors are 1 and itself. Numbers that have more than two factors are composite numbers. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. The number 1 is not a prime number. Write a well-documented, Python program - the main program, that accepts both a lower and upper integer from user entries. Construct a function, isPrime(n), that takes...
a) Write a program that uses a while loop to print all divisors of a number supplied by the user. The program should also print the sum of all the divisors and whether the number the user entered is a prime number or not. Note: The definition of a divisor is a number that divides another evenly (i.e., without a remainder) and the definition of a prime number is a number whose only divisors are 1 and itself. b) Implement...
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...
A perfect number is a positive integer that equals the sum of all of its divisors (including the divisor 1 but excluding the number itself). For example 6, 28 and 496 are perfect numbers because 6=1+2+3 28 1 + 2 + 4 + 7 + 14 496 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 Write a program to read a positive integer value, N, and find the smallest perfect number...
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...
Find the smallest positive integer that has precisely n distinct prime divisors. 'Distinct prime divisor'Example: the prime factorization of 8 is 2 * 2 * 2, so it has one distinct prime divisor. Another: the prime factorization of 12 is 2 * 2 * 3, so it has two distinct prime divisors. A third: 30 = 2 * 3 * 5, which gives it three distinct prime divisors. (n = 24 ⇒ 23768741896345550770650537601358310. From this you conclude that you cannot...
A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...