Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

Recursion using main() function

Revision en1, by TryOmar, 2022-06-07 12:07:50

Hello guys.. I was solving a simple problem that asked me to find the sum of n numbers So I thought about solving this problem with recursion And since I don't like making a lot of functions, instead I decided to use the main function. And yeah that program worked in an efficient way ✅ If you want to try it here it is (just don't use freopen)

#include <bits/stdc++.h>

using namespace std;
int n = 0, x;
int main() {
	//Made By Benjamin007
	static long long sum = 0;
	static int i = 0;
	if (n == 0)
		cin >> n;
	i++;
	if (i <= n) {
		cin >> x;
		sum += x;
		if (i == n)cout << sum;
		return main();
	}
	return 0;
}

Tags recursion, easy, new

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English TryOmar 2022-06-07 12:07:50 692 Initial revision (published)