Chapter - 6: Functions And Pointers

Write a recursive function to obtain the running sum of the first 25 natural numbers.


G
Sections
7
Exercises
#include<stdio.h>
#include<conio.h>

int sum(int);

int main()
{
	int ans = sum(25);
	printf("Running sum of the numbers : %d", ans);
	_getch();
	return 0;
}

int sum(int x)
{
	if (x == 0)
		return x;
	x = x + sum(x - 1);
	return x;
}

© 2021 Garbage Valuegarbage value logo