Write a complete C++ program that does the following: 1. The program will contai
ID: 646753 • Letter: W
Question
Write a complete C++ program that does the following:
1. The program will contain 4 functions:
a. Function Fibonacci(int n) calculates the nth Fibonacci number.
b. Function isPrime(int n) checks if n is a prime number.
c. Function number(int n) that prints the numbers in sequential order based on number passed in. For example if n = 6, the output would be 123456.
d. Function spaces(int n) that prints the n copies of spaces.
2. The main program should utilize the Fibonacci(int n) and isPrime(int n) to print out the first 6 Fibonacci numbers that are also prime.
Explanation / Answer
#include <iostream>
using namespace std;
void number(int number);
int Fibonacci(int n);
bool isPrime(int number);
int main()
{
cout<<"please enter number";
int n;
cin>>n;
number(n);//enter 15 as input
return 0;
}
int Fibonacci(int n)
{
if (n == 0 || n == 1)
return n;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
bool isPrime(int number){
if(number < 2) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(int i=3; (i*i)<=number; i+=2){
if(number % i == 0 ) return false;
}
return true;
}
void number(int number){
for(int i=1;i<=number;i++){
int fib = Fibonacci(i);
if(isPrime(fib)){
cout<<"number is"<<fib<<" ";
}
}
}
void spaces(int number){
for(int i=1;i<=number;i++){
cout<<" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.