0% found this document useful (0 votes)
11 views2 pages

Program 15

The document outlines a program to calculate the sum of the first 20 natural numbers using a recursive function. It includes an algorithm detailing the recursive process, a flowchart, and the source code in C. The program prompts the user for a positive integer and calculates the sum using a loop, displaying the result at the end.

Uploaded by

Devanshu Jaat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Program 15

The document outlines a program to calculate the sum of the first 20 natural numbers using a recursive function. It includes an algorithm detailing the recursive process, a flowchart, and the source code in C. The program prompts the user for a positive integer and calculates the sum using a loop, displaying the result at the end.

Uploaded by

Devanshu Jaat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DEVANSHU 1324035

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

2] flow chart- START

Call sum (n)

Add n to result

Return sum(n)

end
SOURCE CODE-
#include <stdio.h>
int main() {
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (i = 1; i <= n; ++i) {


sum += i;
}

printf("Sum = %d", sum);


return 0;
OUTPUT-

You might also like