Challenge: Write a function, that delivers following output: "1 2 3 4 5 4 3 2 1".
Rules:
- You only can use: 1 for loop, 2 int variables.
- You must not use: IF terms, another function.
- Do not hardcode the output (do NOT do:
print("123454321")
STOP and think about a solution.
Solution:
1 2 3 4 5 6 7 |
int main() { int x = 1; for (int i = 0; i < 9; i++, x++) cout << x - ((i / 5)*(2*(i-4))); //123454321 return 0; } |
Other Solutions in other languages:
external-link <- I loved very much the solution where a Linux date int
is used!
Leave a reply