Program 15
Program 15
PROGRAM NO.15
15: Write a program to calculate the sum of first 20 natural numbers using
recursive function.
Algorithm:
1. Start: Begin the process of calculating the sum of the first 20 natural numbers (for
this problem, n=20n = 20n=20).
2. Define the Recursive Function:
Input: A number 20(natural number).
3. Recursive Calls: The function will repeatedly call itself with a smaller value of natural
(i.e., n−1n - 1n−1) unit it reaches the base case where n=1n = 1n=1.
4. Return the Sum: Once the base case is reached, the sum is calculated as the
recursion "unwinds", with each call returning the value of the sum.
5. Output: over the recursion completes, the sum of the first 20 natural numbers is
printed.
6. stop
Add n to result
Return sum(n)
end
SOURCE CODE-
#include <stdio.h>
int main() {
int n, i, sum = 0;