recursions and recursive functins in c++

 //code for factorial

#include<iostream>
using namespace std;

int factorial(int y)
{
    if(y<=1){
        return 1;   //ye nhi btaenge to infinitely chalta rahega
    }
    return y * factorial(y-1);
}

int fib(int y)
{
    if(y<=2){
        return 1;
    }
    return fib(y-1)+fib(y-2);
}

// fibonacci sequence

int main(){

    int x;
    // cout<<"enter the number whose factorial u wanna find ";
    // cin>>x;
    // cout<<"the factorial of "<<x<<" is "<<factorial(x)<<endl;
    cout<<"enter the term u wanna find out in the fibonacci sequence"<<endl;
    cin>>x;
    cout<<"the term in fibonacci sequence at position "<<x<<" is "<<fib(x);
    return 0;
}

Comments

Popular posts from this blog

Function Overloading in C++

oops recall, basic operations and nesting of member functions

OBJECT ORIENTED PROGRAMMING, classes public and private access modifiers (syntax)