error in 'if' checking

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main (void) {
	int x = 0;
	float y  = 0.0;
	
	scanf("%d%f",&x, &y);
	
	if (!( (x > 0) && (x<=2000) && (y>=0) && (y<=2000) ) )
		;
	else{
	
		if (x%5!=0 || x+.50>y)
			printf("%.2f\n", y);
		else 
			printf("%.2f\n", y-x-.50);
	}
	
	return 0;
}

For the following input: 14.50 15.00 the answer is supposed to come as 15. However it is coming as 0.50 which is apparently wrong coz as x is an integer program reads 14.5 as 14 and the condition 'x%5!=0' happens to be true. Hence it should execute the first print but it is executing the second print. Can anyone tell me why?
closed account (z05DSL3A)
if you put in 14.5 and 15.0 into the program x will be 14 and y will be 0.5 and y is being printed.

It takes the first int out of the input stream, leaving
.5 15.0
, it then take the first float that is .5.
Last edited on
Thanks!! Didnt know integer truncation was done this way.
Topic archived. No new replies allowed.