0

Avg. Two number

21st Oct 2016, 11:00 AM
shanmugaraj
3 Respostas
+ 5
I would use Zen's answer as it won't throw away any remainder. for example the average of 5 and 4 is 9/2 which is 4.5. Leslie's will output 4. Zen's will output 4.5. if you don't care for the remainder, Leslie's is fine. you could also just cast Leslie's to a double by doing this: (double)(a+b)/2
21st Oct 2016, 8:04 PM
Zeke Williams
Zeke Williams - avatar
+ 3
int a, b; cin >> a >> b; cout << (a + b)/2.0 << endl;
21st Oct 2016, 11:08 AM
Zen
Zen - avatar
+ 2
#include <iostream> using namespace std; int main() { int a, b; cin >> a; /*first number*/ cin >> b; /*second number*/ cout << (a+b)/2 << endl; return 0; }
21st Oct 2016, 11:17 AM
Leslie Aula
Leslie Aula - avatar