- 2
Write a C++ code to sum five integer numbers entered into your program by the user, using only two variables in your solution?
.
14 Respostas
+ 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
0
Show your attempt please
0
i can´t see any solution how can i see
0
the user should be enter the same number but i wanna user enter the diffierent number
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.
0
it does not work your solution
0
error for (expect primary-expression befor´:´token)
0
thank you very very much bro❤
0
Sanjay Kamath how can you read from console and manipulate data without saving values in variable? Am I missing something?
0
Michał Doruch
cout<< "The first number is :\n";
cin >> 5;
cout<< "The second number is :\n";
cin >> 7;
And so on ...
0
Sanjay Kamath unfortunately, it does not work this way xD
- 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;
}
- 2
You don't need any variables at all
Just read the five numbers from the console and use cout <<
- 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.