Problem
Write a program in C to find the sum of natural numbers using recursive function.
How to find the sum of natural numbers using recursion in C ?
#include <stdio.h> int Sum(int n); int main() { int input; printf("Abundantcode.com Coding samples\n"); printf("Enter the limit : "); scanf("%d", &input); printf("Result = %d",Sum(input)); return 0; } int Sum(int input) { if(input != 0) return input + Sum(input-1); else return input; }
Output
Abundantcode.com Coding samples
Enter the limit : 5
Result = 15
Leave a Reply