Homework Lab 28 Passing arguments byValue and using return
// Homework Lab 28
// Passing arguments by-Value and using return types.
#include<iostream>
using namespace std;
// TODO: Declare function prototype
// - Name: isPrime
// Parameters:
// - 1 int for the number you are trying to determine \"primeness\" (pass by value)
// Returns: a bool - true if number is prime, false otherwise
// ********************************************
int main()
{
// Declare program variables.
char goAgain;
int numberToTest;
bool numberIsPrime;
// Loop as many times as the user would like
do {
// Prompt the user to enter a number and read their answer
do {
cout << \"What positive whole number would you like to test to see if it is prime: \";
cin >> numberToTest;
} while (numberToTest <= 0); // the number to test must be greater than 0
// TODO: Call function to determine if the user\'s number is a prime
// Pass one argument representing the number to be tested
// Assign the returned value to the correct variable
// numberIsPrime =
// Display output
if (numberIsPrime)
cout << numberToTest << \" is a prime number.\" << endl;
else
cout << numberToTest << \" is NOT a prime number.\" << endl;
cout << endl;
// Ask the user if they want to test another number and read their answer
cout << \"Do you want to test another number (y/n): \";
cin >> goAgain;
} while (tolower(goAgain) == \'y\'); // Repeat until the user answers something other than Y or y.
cout << \"End program - \";
return 0;
}
// ********************************************
// TODO: Define isPrime()
// Refer to the function prototype above for parameter and return type info
//
// Declare a variable to hold result, initialize to true (number is prime until we prove otherwise)
//
// Loop from 2 to the number being tested (do NOT use =)
// - If the number is evenly divisible by the loop counter (think about how to use % operator to do this)
// Set the result variable to false (this number is NOT prime)
//
// Return the result variable
/* Sample Output
What positive whole number would you like to test to see if it is prime: -10
What positive whole number would you like to test to see if it is prime: 0
What positive whole number would you like to test to see if it is prime: 3
3 is a prime number.
Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 4
4 is NOT a prime number.
Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 5
5 is a prime number.
Do you want to test another number (y/n): y
What positive whole number would you like to test to see if it is prime: 67
67 is a prime number.
Do you want to test another number (y/n): n
End program - Press any key to continue . . .
*/
Solution
#include<iostream>
using namespace std;
// TODO: Declare function prototype
// - Name: isPrime
// Parameters:
// - 1 int for the number you are trying to determine \"primeness\" (pass by value)
// Returns: a bool - true if number is prime, false otherwise
bool isPrime(int); //function prototype
// ********************************************
int main()
{
// Declare program variables.
char goAgain;
int numberToTest;
bool numberIsPrime;
// Loop as many times as the user would like
do {
// Prompt the user to enter a number and read their answer
do {
cout << \"\ What positive whole number would you like to test to see if it is prime: \ \";
cin >> numberToTest;
} while (numberToTest <= 0); // the number to test must be greater than 0
// TODO: Call function to determine if the user\'s number is a prime
// Pass one argument representing the number to be tested
// Assign the returned value to the correct variable
numberIsPrime = isPrime(numberToTest);
// Display output
if (numberIsPrime)
cout << numberToTest << \" is a prime number.\ \" << endl;
else
cout << numberToTest << \" is NOT a prime number.\ \" << endl;
cout << endl;
        // Ask the user if they want to test another number and read their answer
        
      
cout << \"Do you want to test another number (y/n): \";
cin >> goAgain;
} while (tolower(goAgain) == \'y\'); // Repeat until the user answers something other than Y or y.
cout << \"End program - \";
return 0;
}
// ********************************************
// TODO: Define isPrime()
// Refer to the function prototype above for parameter and return type info
bool isPrime(int number)
 {
 bool result =true; //    Declare a variable to hold result, initialize to true (number is prime until we prove otherwise)
    int i;
       for(i=2;i<=number;i++) //    Loop from 2 to the number being tested (do NOT use =)
        {
        if(number % i == 0)    //       - If the number is evenly divisible by the loop counter (think about how to use % operator to do this)
        result=false;            //            Set the result variable to false (this number is NOT prime)
        break;
        }
         return result;       //      Return the result variable
         
 }
output:
Success time: 0 memory: 3472 signal:0




