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
- Program in C to calculate the sum of nth natural numbers.
- Program in C and C++ to find sum of nth numbers Using goto statement
Recursive function
- Program in C and C++ to print reverse of a given number using recursive function
- Program in C and C++ to print reverse of a given string using recursive function
- Program in C and C++ to print Nth natural numbers in reverse order using recursive function
- Program in C and C++ to find sum of digits of a given number using recursive function
0 Comments