You will be implementing the following functions. You may modify the main to tes
ID: 3675943 • Letter: Y
Question
You will be implementing the following functions. You may modify the main to test the functions, but use this program below the "Function Protoypes" will work as a starting point.
//********** Function Prototypes ******************
//function isPrime
//input parameter - positive integer greater than 1
//returns true if the number is prime, otherwise false
//
bool isPrime (int number);
//function findPrimes
//input parameter - positive integer
//Uses the isPrime method to print a list of prime numbers between 1 and n.
void findPrimes (int n);
//function findFibo
//input parameter - positive integer
//returns the nth fibonacci number where
//Fib(0) -> 0
//Fib(1) -> 1
//Fib(N) -> Fib(N-2) + Fib(N-1)
//Note: You must use a loop in your solution. Also, if passed a 0, return 0.
int findFibo (int n);
//function findFactors
//input parameter - positive integer
//prints the prime factors of the given number to standard output (cout)
//example output: 52=2*2*13 (or 52=1*2*2*13) or 13=prime
//Hint: You will need nested loops for your solution.
void findFactors (int number);
//***** YOUR ACTUAL PROGRAM ****
Explanation / Answer
1)
bool isPrime (int number)
{
for (int i = 2; i <= number/2; i++)
{
if (number % i) return false;
}
return true;
}
2)
void findPrimes (int n)
{
for (int i = 1; i <= n; i++)
{
if (isPrime(i)) cout << i << " ";
}
}
4)
void findFactors (int number)
{
for (int i = 2; i <= number/2; i++)
{
if (!(number % i) && isPrime(i))
{
cout << i << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.