Friday, May 31, 2013

Sum Of Two Numbers

SOURCE CODE

class addition {
public static void main(String args[]) { // start of main() method.
    int a=10, b=14, c; // initializing a & b of type integer.
    c = a + b; // adding a & b using "+" operator and storing in c.
    System.out.println("Addition : "+c);     // printing result
}         // ending of main() method.
}      // end of the class.


OUTPUT

Addition : 24






NOTE:-
   Instead of using
                c = a + b;
System.out.println("Addition : "+c);
you can use following single line of code

                System.out.println("Addition : "+(a+b));
If you want to use above single line then you don't need to declare the third variable i.e. c.

0 comments:

Post a Comment

    Translate

    Protected by Copyscape