I need help write this program in C++ using modules (functions) that return a bo
ID: 664070 • Letter: I
Question
I need help write this program in C++ using modules (functions) that return a boolean and string module
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 named isPrime, which takes an integer as an argument and returns True if the argument is a prime number, or False otherwise. Use the function in a program that prompts the user to enter a number and then displays a message indicating whether the number is prime.
Below is the pseudocode i wrote
function Boolean isPrime(Integer num)
//Declare the variables
Declare Integer ctr,half
Declare Boolean found
//calculate half of the number and initialize found
Set half = num / 2
Set found = True
//Iterate a For loop starting from 2 to half since any
//number is divided by 1.but to divide a number and
//producing a remainder 0, the divider should lie within
//the range from 2 to half to its value.
//Example 10 can be divisible within a range of 2 to 5
For ctr = 2 to half
//Determine wether num is prime. If not prime, set
//found to False, and exit the loop, otherwise set found
//to True and iterate until loop ends or num is found
If num MOD ctr ==0 Then
Set found = False
Exit for
Else
Set found = true
End If
End For
//Returns found
Return found
End function
Module main()
//Declare the variables
Declare Integer n
Declare Boolean prime
//Read the number and store it in n
Display "Please Provide a number."
Input n
//Calculate n uis prime or not by using function isPrime
Set prime = isPrime(n)
//Determine the status of prime and display message
//accordingly
If prime ==True then
Display n,"is prime."
End If
End MOdule
Explanation / Answer
#include<iostream>
using namespace std;
bool isPrime(int num){
//Declare the variables
int half,ctr;
bool found;
//calculate half of the number and initialize found
half=num/2;
found=true;
//Iterate a For loop starting from 2 to half since any
//number is divided by 1.but to divide a number and
//producing a remainder 0, the divider should lie within
//the range from 2 to half to its value.
for(ctr=2;ctr<=half;ctr++){
//Determine whether num is prime. If not prime, set
//found to False, and exit the loop, otherwise set found
//to True and iterate until loop ends or num is found
if(num%ctr==0){
found=false;
break;
}else{
found=true;
}
}
//Returns found
return found;
}
int main(){
//Declare the variables
int n;
bool prime;
//Read the number and store it in n
cout<<"Provide a number:";
cin>>n;
//Calculate n is prime or not by using function isPrime
prime=isPrime(n);
//Determine the status of prime and display message
//accordingly
if(prime==true){
cout<<n<<" is prime";
}else{
cout<<n<<" is NOT prime";
}
}