English

What will be the output of the given Java code after the calculation [note the data types]? int b = 300, a = 150 ; double w = 0,p = 0; w = b/a; p = a/b; System.out.println ("W=" +w+ "P=" +p);

Advertisements
Advertisements

Question

What will be the output of the given Java code after the calculation [note the data types]?

int b = 300, a = 150 ; double w = 0,p = 0; 

w = b/a;

p = a/b;

System.out.println ("W=" +w+ "P=" +p);

Sum
Advertisements

Solution

w = b/a;

  • b and a are both integers (int), so Java performs integer division first: `300/150` = 2.
  • The result (2) is then assigned to the double variable w, converting it to 2.0.

p = a/b;

  • Both are integers `150/300` = 0.5. 
  • The integer result (0) is assigned to the double variable p, converting it to 0.0.
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Revision of Class 9 Syllabus - Exercises [Page 46]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 1 Revision of Class 9 Syllabus
Exercises | Q 5. (iii) | Page 46
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×