In my last post I have shared about How to add Integer numbers in Java.
Here I have shown how to add and work with float type variables in java.
In the given example I have taken two float type variable as input and then add it.
Code Example
import java.io.*;
class FloatDemo
{
public static void main(String args[])
{
float floatNumber=0.0f;
float floatNumber1=0.0f;
float sumOf=0.0f;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println(“\n”);
System.out.println(“************************************”);
System.out.println(“\n”);
System.out.println(“Enter 1st Floating point input:”);
floatNumber=Float.valueOf(br.readLine()).floatValue();
System.out.println(“\n”);
System.out.println(“Enter 2nd Floating point input:”);
floatNumber1=Float.valueOf(br.readLine()).floatValue();
System.out.println(“\n”);
sumOf=floatNumber+floatNumber1;
System.out.println(“After Addition of the 2 float number =”+sumOf);
System.out.println(“\n”);
System.out.println(“************************************”);
}
catch(Exception e)
{
Exception newEx=new Exception(“Error at:”+new java.util.Date()+””,e);
newEx.printStackTrace();
}
}
}