Write a Program to Find the Sum of Natural Numbers using Recursion.
C Program
#include <stdio.h>int sumRec(int n){if (n == 1)return 1;return n + sumRec(n - 1);}int main(){int n;printf("Enter a number : ");scanf("%d", &n);int sum = sumRec(n);printf("Sum of natural number till %d is %d", n, sum);return 0;}
Input:
Enter a number : 10
Output:
Sum of natural number till 10 is 55
No comments:
Post a Comment