- 2

Write a C++ code to sum five integer numbers entered into your program by the user, using only two variables in your solution?

.

22nd Apr 2021, 11:13 PM
Younis Ahmad
Younis Ahmad - avatar
14 odpowiedzi
+ 2
Sure it works, try that: #include<iostream> using namespace std; int main() { int num; int sum(0); cout<<"type first integer number"; cin>>num; sum+=num; cout<<"type second integer number"; cin>>num; sum+=num; cout<<"type third integer number"; cin>>num; sum+=num; cout<<"type fourth integer number"; cin>>num; sum+=num; cout<<"type fifth integer number"; cin>>num; sum+=num; cout<<"the sum of the number"<<endl; cout<<sum; return 0; } Anyway, you can use loop instead of writing the same thing 5 times
23rd Apr 2021, 12:08 AM
Michal Doruch
0
Show your attempt please
22nd Apr 2021, 11:43 PM
Michal Doruch
0
i can´t see any solution how can i see
22nd Apr 2021, 11:45 PM
Younis Ahmad
Younis Ahmad - avatar
0
the user should be enter the same number but i wanna user enter the diffierent number
22nd Apr 2021, 11:53 PM
Younis Ahmad
Younis Ahmad - avatar
0
Younis Ahmad ,it may work like that, but you have to add "num" to "sum" after every input: cin>>num; sum+=num; What you did is just adding the last input 5 times.
22nd Apr 2021, 11:55 PM
Michal Doruch
0
it does not work your solution
23rd Apr 2021, 12:03 AM
Younis Ahmad
Younis Ahmad - avatar
0
error for (expect primary-expression befor´:´token)
23rd Apr 2021, 12:04 AM
Younis Ahmad
Younis Ahmad - avatar
0
thank you very very much bro❤
23rd Apr 2021, 12:17 AM
Younis Ahmad
Younis Ahmad - avatar
0
Sanjay Kamath how can you read from console and manipulate data without saving values in variable? Am I missing something?
24th Apr 2021, 5:11 AM
Michal Doruch
0
Michał Doruch cout<< "The first number is :\n"; cin >> 5; cout<< "The second number is :\n"; cin >> 7; And so on ...
24th Apr 2021, 5:24 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Sanjay Kamath unfortunately, it does not work this way xD
24th Apr 2021, 5:31 AM
Michal Doruch
- 1
#include<iostream> using namespace std; int main() { int num; int sum; cout<<"type first integer number"; cin>>num; cout<<"type second integer number"; cin>>num; cout<<"type third integer number"; cin>>num; cout<<"type fourth integer number"; cin>>num; cout<<"type fifth integer number"; cin>>num; sum=num+num+num+num+num; cout<<"the sum of the number"<<endl; cout<<sum; return 0; }
22nd Apr 2021, 11:51 PM
Younis Ahmad
Younis Ahmad - avatar
- 2
You don't need any variables at all Just read the five numbers from the console and use cout <<
24th Apr 2021, 5:09 AM
Sanjay Kamath
Sanjay Kamath - avatar
- 2
Write C++ program that allows to take user input five times and calculate the sum of the numbers user has entered. Use while loop.
13th Oct 2022, 11:47 AM
ABDURASHID ABDULAHATOV