MY name is ruhul amin

ISLAMIC UNIVERSITY OF KUSHTIA

Saturday, March 30, 2013

recersive function

#include <iostream>
using namespace std;
class name
{
  private:
  int p;
  public:
  int sum(int n);
};
int name::sum(int n)
{
    if(n==0)
    return 0;
    else
    return (sum(n-1)+n);
}
int main()
{
    name ob;
    int a;
    while(cin>>a)
    {
    cout<<ob.sum(a)<<endl;
    }
    return 0;
}

No comments:

Post a Comment