Arithmetic operator with different data types Output Writing – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Solved Problem #JAVA#3669 siteicon   siteicon   siteicon  

Problem Statement - Arithmetic operator with different data types Output Writing

Write output of the following program instructions.

int a=5;
float b=9.0f;
double c= a+b;
double d=b+a/2;


TC++ #3669

Run Output


int a=5; //Integer variable a initialised with value 5

float b=9.0f; //Float variable b initialised with value 9. Notice the use of f suffix. This ensures that the storage space for literal is also of float type.

double c= a+b;
Here right side has an integer variable a added to a float variable and the variable type is implicitly promoted to double type variable c.

double d=b+a/2;
Since a is an integer so a/2 would be an integer division and hence a/2 would work as 5/2 resulting in 2 (and not 2.5) then it is added with 9.0 float to make it 11.0 float which is promoted to double variable d.

This will print c as 14.0 and d as 9.0


  • In Java for a double initialisation we may not give the suffix f or d but if we add decimal followed by zero for a float initialisation f suffix is essential. For e.g.
    float b=9; is okay
    float b=9.0; is not okay it should be float b=9.0f;
    double b=9; is okay
    double b-9.0; is also okay

Common Errors

  • Students often commit mistakes in integer division such as 5/2 would be 2 and not 2.5 while 5.0/2 would be 2.5 as numerator is already a double value.


sunmitra| Created: 2-Mar-2018 | Updated: 27-Nov-2019|