Product and Division of two numbers – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #JAVA#571 siteicon   siteicon   siteicon  

Product and Division of two numbers

Product and Division to show data type compatibility difference.

Learning Objectives

To learn java concepts related to :

  • Initializing variables dynamically.
  • Compatibility of data types.

Program Approach

The objective is to demonstrate the dynamic initialization and declaration of variables along with the compatibility of the data types with certain type of numerical values.

We will be first declaring two variables with their initial values. Then we will be initializing a third variable with the product and the fourth variable with division of the stored values. The fourth variable will be of double data type as we do not know whether the outcome of the division would be an integer or real number.

Source Code

TC++ #571

Source Code

Run Output

The product is : 100
The division is : 4.0

Code Understanding

int num3=num1*num2;

In this line we have initialized the variable sum with the product of the values stored in the variables num1 and num2.

double num4=num1/num2;

In this line we have initialized the variable num4 with the division of the values stored in the variables num2 and num1. The data type for this variable is double as we are not sure whether the outcome of the division will be an integer or a real number. Also the right side should also be double data type. For this at least the numerator should be double. For this we can write
double num4 = (num1*1.0)/num2; This will make the numerator of right hand side as double and now the calculations would be perfect. To test this you can check with num1 set as 21.


Suggested Filename(s): ProductDivision.java



Share

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






Back