division of floats

Upload: kyle-chadee

Post on 25-Feb-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Division of Floats

    1/1

    public class FloatsandIntsExample {public static void main(String[] args) {

    int x = 5, y = 7; float a = 5, b = 7; System.out.printf("%d\n", y/x); //gives 1 (truncation) System.out.printf("%.2f\n", (float)y/x); //1.40 - cast forced float division (exact answer) System.out.printf("%.2f\n", b/a); //1.40 - a and b are floats so a floatdivision is done. //Correct answer obtained. System.out.printf("\nDone."); } //end main}/*OUTPUT11.401.40*/