how to print 1 to 100 without loop,iteration,goto. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to print 1 to 100 without loop,iteration,goto.

23rd Sep 2016, 2:20 AM
SRINIVAS G
2 Answers
+ 6
So you use a concept of recursion if you do not want to use a loop. Pretty much the function calls itself again until it gets to the point to break it. Here is example code #include <iostream> using namespace std; void count(int start) { if(start <= 100) { cout << start << endl; start++; count(start); } } int main() { int start = 1; count(start); }
23rd Sep 2016, 4:03 AM
Catlin
Catlin - avatar
0
trick: use foreach "function" #include <iostream> #include <numeric> #include <algorithm> using namespace std; int main() { int oneToHundred[100]; iota(oneToHundred, oneToHundred + 100, 1); for_each(oneToHundred, oneToHundred + 100, [](int i){cout << i << endl;}); return 0; }
23rd Sep 2016, 1:59 PM
kiwiyou
kiwiyou - avatar