You will be implementing the following functions. You may modify the main to tes
ID: 3926154 • 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) {
//YOUR CODE GOES HERE
if(number<=1)
return false;
int i=2;
for(i=2;i<number;i++)
if ((number%i)==0)
return false;
return true;
}
//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) {
//YOUR CODE GOES HERE
int i=1;
for(i=1;i<=n;i++)
{
if(isPrime(i)==true)
cout << i << " ";
}
cout << endl;
}
//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) {
//YOUR CODE GOES HERE
if(n==0)
return 0;
if(n==1)
return 1;
int x = 0,y = 1;
int z;
int i=2;
while(i<=n)
{
z = x+y;
x = y;
y = z;
i++;
}
return z;
}
//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 CODE GOES HERE
int i=2;
bool flag = false;
while(number>1)
{
if(isPrime(i)==true)
{
if((number%i) == 0)
{
while((number%i)==0)
{
if(flag == false)
{
cout << number << " = " << i;
flag = true;
}
else
cout << "*" << i;
number = number/i;
}
}
}
i++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.