Problem Statement:
Write a recursive function to obtain the running sum of first
25 natural numbers.
25 natural numbers.
Solution:
//Ahmad Furqan
//P5.e
#include <iostream>
#include <conio.h>
using namespace std;
int sum_nat(int num)
{
if (num == 25)
return num;
return num + sum_nat(num + 1); //return current num + sum of numbers above this num and upto 25
}
void main(void)
{
cout << "Sum of first 25 Natural Numbers is: ";
cout << sum_nat(1);
_getch();
}
No comments:
Post a Comment