programming-examples/c/Functions/C Program to Write a recursive function to calculate 1+2+3+...+n..c

30 lines
489 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/* Sum of natural numbers - Write a recursive function to calculate 1+2+3+...+n */
#include <stdio.h>
#include <conio.h>
long sum(int n)
{
if(n==1)
return 1 ;
else
return ( n + sum(n-1) ) ;
}
void main()
{
int n ;
clrscr() ;
printf("Enter n: ") ;
scanf("%d", &n) ;
printf("Sum of first %d natural numbers is %ld", n, sum(n) ) ;
getch() ;
}
/*
Output:
Enter n: 3
Sum of first 3 natural numbers is 6
*/