You will be implementing the following functions. You may modify the main to tes
ID: 3926157 • Letter: Y
Question
You will be implementing the following functions. You may modify the main to test the functions, but this programwill 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);
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
//********** 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);
//****************** MAIN *****************************
int main ()
{
int testNum;
//test for the isPrime function
cout << "Enter a test number - ";
cin >> testNum;
cout << endl;
if (isPrime(testNum))
cout << testNum << " is prime." << endl;
else
cout << testNum << " is not prime." << endl;
//test for how many primes
cout << "Enter n to print the prime numbers between 1 and n: ";
cin >> testNum;
cout << endl;
findPrimes(testNum);
cout << endl;
//test for Fibonacci number finder
cout << "Which Fibonacci number? ";
cin >> testNum;
cout << endl;
cout << "The " << testNum << " Fibonacci number is : " << findFibo(testNum) << endl;
cout << endl;
//test for prime factors
cout << "Factor what number: ";
cin >> testNum;
cout << endl;
findFactors(testNum);
return 0;
}
//function isPrime
//input parameter - positive integer greater than 1
//returns true if the number is prime, otherwise false
//
bool isPrime (int number)
{
int c, f = 0;
if(number > 1)
{
for(c = 2; c < number; c++)
{
if(number %c == 0)
{
f = 1;
break;
}
}
if(f == 0)
return true;
else
return false;
}
else
{
cout<<" Error: Number must be positive integer greater than 1 ";
}
}
//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)
{
int c;
if(n > 1)
{
for(c = 2; c < n; c++)
{
if(isPrime(c))
cout<<" Prime: "<<c;
}
}
else
{
cout<<" Error: Number Must be positive integer greater than 1";
}
}
//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)
{
int t1 = 0, t2 = 1, t3, co = 2;
if(n < 0)
{
cout<<" Error: Number must be positive integer ";
}
else if(n == 0)
{
return 0;
}
else
{
for(; co <= n; co++)
{
t3 = t1 + t2;
t1 = t2;
t2 = t3;
}
return t3;
}
}
//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)
{
int c, d;
if(number < 1)
{
cout<<" Error: Number must be positive integer greater than 1";
}
else
{
for(c = 2; c < number; c++)
{
if(number % c == 0)
{
if(isPrime(c))
cout<<c<<" = prime "<<endl;
}
}
}
}
Output:
Enter a test number - 5
5 is prime.
Enter n to print the prime numbers between 1 and n: 10
Prime: 2
Prime: 3
Prime: 5
Prime: 7
Which Fibonacci number? 8
The 8 Fibonacci number is : 21
Factor what number: 15
3 = prime
5 = prime
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.