A challenging programming excercise for beginners | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

A challenging programming excercise for beginners

So, I came across a challenging programming excercise. And I think it is a good practice for beginner/intermediate programmers. "Write a program that outputs all possibilities to put + or - or nothing between the numbers 1,2,…,9 (in this order) such that the result is 100. For example 1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100." Succes!

3rd May 2017, 4:28 PM
Joep
Joep - avatar
5 Answers
+ 4
This is extremely easy, there are only 3^8 possibilities, hence brute force will work. Still a nice exercise for beginners.
3rd May 2017, 4:32 PM
Tob
Tob - avatar
+ 4
I think this should be correct, but I did not really check it, as I'm quite tired right now. Quite a short and not too inelegant code, although the approach is rather naive: https://code.sololearn.com/ctbncskQ1Nxi
4th May 2017, 2:58 AM
Tob
Tob - avatar
+ 4
@Krishneel Nair: I think you should read the problem again. All your code does is to print the numbers up to the user input and put alternating signs in between.
4th May 2017, 11:34 PM
Tob
Tob - avatar
+ 1
in C++ int user_input = 0; cin >> user_input ; for (int i = 1; i <= user_input; i++) { if (i == user_input ) { cout << i << " = "; } else if (i % 2) { cout << i << " - "; } else { cout << i << " + "; } }
4th May 2017, 4:26 AM
Krishneel Nair
Krishneel Nair - avatar
0
@Tobi You might be right. I change it to beginner
3rd May 2017, 4:34 PM
Joep
Joep - avatar