My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

sum of n numbers using recursion

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
int fact(int);
void main()
{
  clrscr();
  int n;
  cout<<"enter the number: ";
  cin>>n;
  cout<<"sum of first "<<n<<" numbers: ";
 cout<<fact(n);
  getch();
}
int fact(int x)
{
  int p;
  if(x>0)
 { p=x+fact(x-1);
  return p;}
  else
  return 0;
}

0 comments: