adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to find sum of nth numbers Using recursive function

Program in C and C++ to find sum of nth numbers Using recursive function
C Program
#include<stdio.h>
 //sum of nth natural numbers using recursive function
 int sumOfNth(int n){
    if(n == 0)
        return 0;
    else
        return (n + sumOfNth(n - 1));
 }

 int main()
  {
    int n;
    printf("Enter a number :- ");
    scanf("%d",&n);
    printf("Sum of %d natural numbers : %d",n,sumOfNth(n));
    return 0;
  }
C++ Program
    #include<iostream>
 using namespace std;
 //sum of nth natural numbers using recursive function
 int sumOfNth(int n){
    if(n == 0)
        return 0;
    else
        return (n + sumOfNth(n - 1));
 }

 int main()
  {
    int n;
    cout<<"Enter a number :- ";
    cin>>n;
    cout<<"Sum of "<<n<<" natural numbers : "<<sumOfNth(n);
    return 0;
  }
Output :-










Related Programs



Recursive function




Post a Comment

0 Comments