C Program to Print Sum of First N Numbers Using Recursion
Recursion in C means a function calling itself. The function dspsum recursively adds the number to the variable sum by decreasing the value of num in every call till it becomes 0.
- //C program to print sum of first n numbers using recursion
-
- #include <stdio.h>
-
- void dspsum(int);
-
- int main()
- {
- int num;
-
- printf("Enter the Nth number: ");
- scanf("%d", &num); //taking input from user
- dspsum(num); //calling the recursive function
- return 0;
- }
-
- void dspsum(int num)
- {
- static int sum = 0;
-
- if (num == 0)
- {
- printf("Sum of first N numbers is %d\n", sum);
- return;
- }
- else
- {
- sum += num;
- dspsum(--num);
- }
Output

Solution not working or have any suggestions? Please send an email to [email protected]
Download Android App