#include <iostream> using namespace std; int SumOfNNaturalNumbers(int n) { if(n == 1) return 1; return (n + SumOfNNaturalNumbers(n-1)); } int main() { int n; cin >> n; cout << "Sum of first " << n << " natural numbers: " << SumOfNNaturalNumbers(n) << endl; return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter