i wrote a program for practice. There is no problem with codes and program runs without giving any error. However, when there is a problem about output.
I tried to rounded the floating number just considering their 2 digits after the decimal points. The program should round up the number if the decimal is .45 or higher and round down the number if the decimal is less than .45 . In general, there is no problem about the outputs, but when the input is 64.45 , 65.45 , 66.46, etc. the number doesn't rounded up; it is rounded down.
The code of program is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float gr;
int x;
float a;
cout << "enter grade: ";
cin >> gr;
a = 100 * gr;
cout << a << endl;
x = a;
cout << x << endl;
x = x % 100;
cout << x << endl;
if (x % 100 <= 44)
{
gr= floor(gr);
}
else
{
gr=ceil(gr);
}
cout << gr << endl;
return 0;
}
|
p.s : when i changed if statement as
if ( x % 100 <= 48)
the problem was again when the input is 64.45,65.45,etc.
p.s2 : there is no problem when the input is 63.45 or less.
p.s3 :
I get a and x as outputs deliberately, in order to see where the problem is.
When the input is 63.45
output:
6345
6345
45
64
But when the input is 64.45
output:
6545
6544
44
65
where first one is output of "a" and second one is output of "x"