C++ Online Compiler
Example: C++ Program to Find Sum of Natural Numbers using Recursion
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// C++ Program to Find Sum of Natural Numbers using Recursion #include <bits/stdc++.h> using namespace std; int recSum(int x); // It's the driver function int main() { int x; // x - denotes a positive number cout << "-----Enter a positive number to get the sum-----\n"; cin >> x; cout << "\nThe Sum is: " << recSum(x) << "\n"; return 0; } // It's the recrsive function // To make the sum of natural numbers int recSum(int n) { if (n) return n + recSum(n - 1); else return n; }
22
Output
Clear
ADVERTISEMENTS