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
|
Run Output
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|