addition of two numbers in java

how to add two numbers in java, how to add two float numbers on console, program for add two numbers


Category: Java J2SE Tags: Java, Java J2SE


        Today i am going to show you how to add two numbers by java on console. go through the code below:
import java.io.*;
public class addition
{
public static void main(String args[])
{
float a,b,c;
DataInputStream input=new DataInputStream(System.in);
System.out.println("enter two numbers for addition");
a=Float.parseFloat(input.readLine());
b=Float.parseFloat(input.readLine());
c=a+b;
 System.out.println("the total is "+c);
}
}
Explanation :-

import :- first line of code, import is a keyword for import packages(a package is collection of many inbuilt classes and functionality can use in our program) and .* means all the classes from the package will be imported.

java.io :- is a package, contains the classes that handle fundamental input and output operations in Java.
float :- datatype represents real numbers.here a,b,c three variable defined.
DataInputStream :- defined in package java.io. This class has method for reading data from the console.
System.in :- represents that we want to read from standard input system i.e. from keyboard.
Float.parse :- typecasting(convert the input stream in float).
input.readLine() :- the readLine method of class DataInputStream allows us to read a string from keyword.

c = a + b performs addition and assign results to c and we can print results.

Like 1 Person
Published on 29 August 2013
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 132
Questions: 9
Given Best Solutions: 9 *

Comments:

alex
We can speed the process in visual manner, no need IO classes in Netbeans, there is a visual editor as Visual Studio for C ASP.NET, clicking on button trigger the event as ASP.NET; so no need to reuse IO classes for teachers :)
Like 0 People on 4 December 2013
Nikhil Joshi
hi alex, i'm founder and developer of this website... i need ur feedback about this site, i think u have visited all features of it.so how is this and what new improvements and features can be make ..
Like 2 People on 7 December 2013

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x