#include <iostream>
usingnamespace std;
int main()
{int N,I,SUM,NUM;
float AVG;
cout<<"N=";
cin>>N;
SUM=0;
for (I=1;I<=N;I++)
{cout<<"NUM=";
cin>>NUM;
SUM=SUM+NUM;
}
if (N>0)
{
AVG=SUM/N;
cout<<"AVERAGE IS "<<AVG;
}
else cout<<"THERE IS NO AVERAGE";
return (0);
}.
well let's say i use 1,2,3,4 as numbers .the average the programm gives me is 2 which is wrong whereas if i move N or SUM or both into float it gives me the correct result 2,5 .shouldn't it give me 2,5 even if i use it is i have written it since AVG is on float ?
in the division SUM/N both variables are of type int.
That means integer divide is done, giving an integer result. After that, the result is assigned to AVG.