Sunday, 29 July 2012

WAP to add two numbers by just assigning values(J2SE).

Adding two numbers
J2SE Programming

Now I am going to write a program which adds two numbers.
For this i just want to know about addition operator and two inputs.
As we know, there is one operator known as plus(+) operator can be used to add two inputs.
Now the problem is how to get input for adding numbers.

 There are three ways to get input as follows:-
  1. One way is just by assigning values.
  2. Second way is by User input during Run-time.
  3. Third way is using Command-Line arguments.
Here we are using First way i.e. by Assigning values.
Other ways will be discussed Later.

                               class Addition
                              {
                                          //Main Function
                                       public static void main(String[] args)
                                      {
                                          Addition a1=new Addition();
                                          int a=a1.add(10,20);
                                          System.out.println(Sum is : +a);
                                      }

                               //Function to add two numbers

                                       public int add(int a,int b)
                                      {

                                         int c=0;
                                         c=a+b;
                                         return c;

                                      }
                              }


Now save it as Addition.java
Compile it using "javac Addition.java" Command in CMD
After compilation Run it using command "java Addition" in CMD

0 comments:

Post a Comment