// If you are not sure what some lines of code do, try looking back at
// previous example programs, notes, or ask a question.
#include <iostream>
using namespace std;
int addFrom(int);
int main() {
char selection;
// A simple do/while loop, where the loop will continue asking the user
// to continue until they enter something that is not 'y'
do {
cout << "Continue? (y/n): ";
cin >> selection;
} while(selection == 'y');
cout << endl << endl;
// A nested for loop, will run the inner loop 10 times. Each time the outer
// loop is run, i is incremented, and hence the first output number goes up
// every ten outputs. Within each run of the outer loop, the inner loop sets
// j to 0, and counts it up to 10, which produces each pair of output numbers.
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
cout << i << "-" << j << " ";
}
cout << endl;
}
cout << endl << endl;
// This calls the recursive function
cout << "Recursion example: " << addFrom(5) << endl << endl;
system("pause");
return 0;
}
// This is a recursive function. See the notes for how it works.
int addFrom(int x) {
if(x == 1)
return 1;
else
return x + addFrom(x - 1);
}
Ly8gSWYgeW91IGFyZSBub3Qgc3VyZSB3aGF0IHNvbWUgbGluZXMgb2YgY29kZSBkbywgdHJ5IGxvb2tpbmcgYmFjayBhdAovLyBwcmV2aW91cyBleGFtcGxlIHByb2dyYW1zLCBub3Rlcywgb3IgYXNrIGEgcXVlc3Rpb24uCgojaW5jbHVkZSA8aW9zdHJlYW0+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGFkZEZyb20oaW50KTsKCmludCBtYWluKCkgewoJCgljaGFyIHNlbGVjdGlvbjsKCQoJLy8gQSBzaW1wbGUgZG8vd2hpbGUgbG9vcCwgd2hlcmUgdGhlIGxvb3Agd2lsbCBjb250aW51ZSBhc2tpbmcgdGhlIHVzZXIKCS8vIHRvIGNvbnRpbnVlIHVudGlsIHRoZXkgZW50ZXIgc29tZXRoaW5nIHRoYXQgaXMgbm90ICd5JwoJZG8gewoJCWNvdXQgPDwgIkNvbnRpbnVlPyAoeS9uKTogIjsKCQljaW4gPj4gc2VsZWN0aW9uOwoJfSB3aGlsZShzZWxlY3Rpb24gPT0gJ3knKTsKCQoJY291dCA8PCBl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