+ 1

How do I add the numbers between two numbers? For example 4 and 8 add me to 5,6,7 and show the result that is 18

I am making an algorithm that if I enter 3 and 6 shows me the numbers that are in the middle of them two, which in this case are 4 and 5, and I want to add the values 4 and 5, how do I add them up? #include <iostream> using namespace std; int main() { int a, b,k; printf("insert num: "); cin >> a; printf("insert num: "); cin >> b; cout << "son: "; for( k = a+1; k < b ; k++){ cout << k << " "; int sum = ?; // cout<< "\n\n" << sum; } }

30th Apr 2020, 4:49 PM
KeynerZzz
KeynerZzz - avatar
3 Antworten
+ 2
sum += k; in your for loop
30th Apr 2020, 4:59 PM
minirkk
minirkk - avatar
+ 1
replace your for loop with this ternary value initialize (a<b? a:b) return minimum of a and b (a>b? a:b) return maximum of a and b for( k=((a<b? a:b) +1) ; k < (a>b? a:b); k++)
30th Apr 2020, 7:50 PM
minirkk
minirkk - avatar
0
I have another problem, if I put 4 and 8, it prints 5,6,7 but if I put 8 and 4, it doesn't print anything ... how do I correct that?
30th Apr 2020, 7:34 PM
KeynerZzz
KeynerZzz - avatar