help: function that displays prime number less than the input

i am struggling with writing a function that displays prime numbers which is less than or equal to the number input (if the input is a prime number.



also i am also supposed to write a fibonacci function that accepts 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.

so far i have this wrong code:


#include <iostream>
#include <cmath>
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
}

}

bool isPrime(int num)
{
if(num < 2 ) return false;
else if(num == 2) return true;
for(int i = 2; i < ceil ( sqrt(num) ); i++)
if(num % i == 0) return false;
return true;
}
void printPrime(const int Max = 100)
{
for(int i = 2; i < MAX; i++)
if( isPrime(i)) cout << i <<" is Prime\n";

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;



}

Topic archived. No new replies allowed.