help prime number function

i am supposed to complete this assignment:

write a program that asks the user for a non-negative positive number greater than 1. The user is re-prompted to enter a valid number if a number less than 1 is entered. The program then outputs: (a) the largest Fibonacci number that is less than or equal to the input and (b) the largest prime number less than or equal to the input.

(a) you must define 3-4 appropriate functions, (b) write proper comments describing critical aspects of your logic and reasoning, (c ), must be accurate.



so far i have:



#include <iostream>

using namespace std;

int fib(int n) // calls fibonacci function
{
if(1 == n || 2 == n)
{
return 1;
}
else
{
return fib(n-1) + fib(n-2);// adds previous fibonacci numbers
}

}

int prime(int k)

{
if (k % 2 == 0|| k % 3 == 0 || k % 5 == 0 )
return k;




}

int main() {

int i;



cout<< "Please enter an integer (number must be greater than 1):"<< endl;
cin>> i;

if( i==1) {
cout<< " Please enter another number"<< endl;
}

else {
cout <<"the closest fibonacci number is" <<fib(i) << endl;//prints fibonacci number

}


cout << " the closest prime number is" << prime(i)<< endl;

return 0;



}

What will your program do if user will enter 0 or negative number?
1
2
3
if( i==1) {
cout<< " Please enter another number"<< endl;
}


What is a prime number? Your function "prime(i)" do not return closest prime number. If user enter 11 what will your function return?

I do not know what is Fibonacci number. Sorry :D
Last edited on
Fibonnaci number is a number in the sequence 1, 1, 2, 3, 5, 8, 13, 21, etc. where the next number in the sequence is the sum of the last two numbers (1+1 = 2, 1+2 = 3, 2+3 = 5, 3+5 =8, 5+8=13, 8+13 = 21, etc)
In this case fib() function are wrong. If I input 9 in fib() function it will return 34, but should be 8.
Topic archived. No new replies allowed.