Average of two double numbers – Computer Sir Ki Class


Lost your password?

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


Code Learning #JAVA#573 siteicon   siteicon   siteicon  

Average of two double numbers

Finding average of two double numbers.

Learning Objectives

To learn java concepts related to :

  • Initializing variables dynamically.
  • Variations while passing arguments in println() method.

Program Approach

The objective is to demonstrate the dynamic initialization of variables.

We will be firstly declaring two variables with their initial values. Then we be dynamically initializing the third variable with the average of the above values.

Source Code

TC++ #573

Source Code

Run Output

Average of 23.5 and 12.5 is 18.0

Code Understanding

double avg=(num1+num2)/2;

Here, we have dynamically initialized the variable avg with the average of the values stored in the variables num1 and num2. Since we were not sure whether the average would be an integer or a decimal number so we have declared it with double data type. If you have declared any expression in double data type and its value comes out to be a perfect integer then also the double data type will add .0after that integer so that it may also become a decimal number.

System.out.println(“Average of “+
num1+” and “+
num2+” is : “+avg);

This statement has been written in three different lines in order to demonstrate that within println() method we can terminate a statement and start it from the next line only after a +operator.

Suggested Filename(s): Average.java


admin| Created: 16-Aug-2016 | Updated: 21-Mar-2018|