+ 3

Variables problem

#include<iostream> using namespace std; int main (){ int a = 5; int b = 2; cin >> a; cin >> b; int sum = a + b; cout<< sum; return 0; } What’s wrong with that code ? Every time I run it it just gives me 14 which Is the wrong answer. It is supposed to be 7.

16th Oct 2022, 8:24 AM
Nada Ahmed
Nada Ahmed - avatar
7 ответов
+ 1
#include<iostream> using namespace std; int main (){ int a = 5; int b = 2; int sum = a + b; cout<< sum; return 0; } Or #include<iostream> using namespace std; int main (){ cin >> a; cin >> b; int sum = a + b; cout<< sum; return 0; }
16th Oct 2022, 8:42 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Why can’t we write cin , declare and assign the value at the same code ?
16th Oct 2022, 9:00 AM
Nada Ahmed
Nada Ahmed - avatar
+ 1
Thanks ✨
16th Oct 2022, 9:03 AM
Nada Ahmed
Nada Ahmed - avatar
+ 1
The second one isn’t working properly
16th Oct 2022, 9:04 AM
Nada Ahmed
Nada Ahmed - avatar
+ 1
You can, only as you see, this leads to incorrect results. although in theory, after entering values ​ manually, the value previously entered in the variable should disappear. try to figure it out why this is happening
16th Oct 2022, 9:34 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
16th Oct 2022, 12:01 PM
Sreeju
Sreeju - avatar
0
Explanation: 20% of 50 is 10. To calculate 20% of a given amount, you can multiply the number by 20 and divide it by 100: 50*20/100 = 10.0 please write code In python
17th Oct 2022, 5:50 AM
Kalyan990