C++ Program to Find Sum Natural Numbers using Recursion
Write C++ Program to Find Sum Natural Numbers using Recursion
// CPP Program to Find Sum Natural Numbers using Recursion #include <iostream> using namespace std; int recSum(int n) { if(n <= 1) return n; return n + recSum(n - 1); } int main() { int n; cout << "Enter n :--> "; cin >> n; cout << "1 + 2 + ... + " << n << " = " << recSum(n); return 0; }
Output:
Enter n :--> 6
1 + 2 + ... + 6 = 21