CLASS - X (ICSE)
COMPUTER APPLICATION
Write a program to calculate a gross salary if basic salary
is given of an employee. His dearness allowance is 40% of basic salary, and
house rent allowance is 20% of basic salary.
class GrossSalary
{
public static void main(String[]
args) {
float bs=10000.0f;
float da,hra,gr_sal;
da=(float)0.4*bs;
hra=(float)0.2*bs;
gr_sal=bs+da+hra;
System.out.println("Basic
salary is : "+bs);
System.out.println("Dearness
allowance is : "+da);
System.out.println("Hra is : "+hra);
System.out.println("Gross
salary is : "+gr_sal);
}
}
Note: In the above example, you will notice that I have written (float) before 0.4*bs; and before 0.2*bs; because when we simply multiply 0.4*bs or 0.2*bs , we will get an answer in decimal but this answer will be considered as of double data type by default, and we want to store that value into float type variable, this will not be possible without explicit conversion.
Hence, explicit conversion is done in this example.
Note: In the above example, you will notice that I have written (float) before 0.4*bs; and before 0.2*bs; because when we simply multiply 0.4*bs or 0.2*bs , we will get an answer in decimal but this answer will be considered as of double data type by default, and we want to store that value into float type variable, this will not be possible without explicit conversion.
Hence, explicit conversion is done in this example.
IMPORTANT TIP
if you don't want to convert it explicitly, then you can take all the variables in double, as you know explicit conversion is needed only when we want to convert the higher data type into lower one.
class GrossSalary
{
public static void main(String[]
args) {
double bs=10000.0;
double da,hra,gr_sal;
da=0.4*bs;
hra=0.2*bs;
gr_sal=bs+da+hra;
System.out.println("Basic
salary is : "+bs);
System.out.println("Dearness
allowance is : "+da);
System.out.println("Hra is : "+hra);
System.out.println("Gross
salary is : "+gr_sal);
}
}
Output:
Basic salary is : 10000.0
Dearness allowance is : 4000.0
Hra is : 2000.0
Gross salary is : 16000.0