About us

C Program for finding the sum of first n natural numbers using Recursion

C Program for finding the sum of first n natural numbers using Recursion


c-program-finding-sum-first-n-natural-numbers-using-recursion



#include<stdio.h>
#include<conio.h>
int sum(int n);

void main()
{
 int num;
 clrscr();
 printf("Enter the nth Number for find sum of first n natural numbers\n");
 scanf("%d",&num);
 printf("\nThe Sum of first %d natural numbers is %d\n",num,sum(num));
 getche();
}

int sum(int n)
{
 if(n==1)
  return 1;
 else
  return(n+sum(n-1));
}


OUTPUT

c-program-finding-sum-first-n-natural-numbers-using-recursion




------------------------------------------------------------------------------------------------------------

You can also visit:-