You will be implementing the following functions. You may modify the main to tes
ID: 3921838 • Letter: Y
Question
You will be implementing the following functions. You may modify the main to test the functions, but this program 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);
please use C++
coding
Explanation / Answer
#include<iostream>
bool isPrime (int number){
for(int i=2;i<number;i++){
if(number%i ==0)
return false;
}
return true;
}
void findPrimes (int n){
for(int i=2;i<=n;i++){
for(int j=2;j<i;j++){
if(i%j==0) break;
}
if(j==i)
cout<<i<<" ";
}
}
int findFibo (int n){
int a=0,b=1,c,count=2;
if(n == 0) return 0;
else if(n==1) return 0;
else if(n == 2) return a;
else{
while(count<n){
c=a+b;
a=b;
b=c;
count++;
}
return c;
}
}
void findFactors (int number)
{
while (number%2 == 0)
{
cout<<"2 ";
number = number/2;
}
for (int i = 3; i < number; i = i+2)
{
while(number%i == 0)
{
cout<<i<<" ";
number = number/i;
}
}
if (n > 2)
cout<< number<<" ";
}
void main(){
cout<<isPrime(47);
findPrimes(35);
cout<<findFibo(6);
findFactors(52);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.