I'm currently trying to learn on how to work with For/Loops, but I have a problem at the moment. I'm trying to figure out how to add in a list of combinations that add to a certain number, for example;
1 + 3 = 4
2 + 2 = 4
3 + 1 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main()
{
for (int i = 1; i < 4; i++)
{
for (int k = 1; k < 4; k++)
{
cout << i << " + " << k << " = " << i + k << endl;
}
}
return 0;
}
But I can't seem to figure out how to execute the math combinations for that one number, it gives me combinations for every number listed below
1 + 1 = 2
1 + 2 = 3
1 + 3 = 4
2 + 1 = 3
2 + 2 = 4
2 + 3 = 5
3 + 1 = 4
3 + 2 = 5
3 + 3 = 6
I'm aware that adding the second "For" makes the numbers repeat more than once, but I can't think if anything else to work with. Any advice?
#include <iostream>
usingnamespace std;
int main()
{
for (int i = 1; i < 4; i++)
{
for (int k = 1; k < 4; k++)
{
if(i + k == 4)
{
cout << i << " + " << k << " = " << i + k << endl;
}
}
}
}