Monday 25 August 2014

Add Two Numbers In Java



You can add two numbers in java by two methods :

1.    By using the parameters at the time of execution of java program which are actually stored in the String array of main method. You can access that parameters by using the name of the array you passed in the main method with the index starting from zero.

The program  is shown below :

1:  class AddTwoNumbers  
2:  {  
3:        public static void main(String[] args)  
4:        {  
5:             int no1, no2;  
6:             //Get the first argument and parse it as int  
7:             no1 = Integer.parseInt(args[0]);  
8:             //Get the second argument and parse it as int  
9:             no2 = Integer.parseInt(args[1]);  
10:            //calculate the sum  
11:            int sum= no1 + no2;  
12:            System.out.println("Sum of two numbers is : "+sum);  
13:        }  
14:  }  

Output of the program :



Make sure that you enter only numbers if you will not enter numbers than it will show you the NumberFormatException because I have parsed the string of numbers as integers in the program. And also you have to enter minimum of two numbers with space at the time of execution of the program. The program takes first two numbers and after adding it will show you the sum of two numbers as output.

2. The second method may be use of Scanner class from java.util package. With the use of Scanner class you can ask to enter the numbers at run time from the user.

The program is shown below :

1:  import java.util.Scanner;  
2:    
3:  class AddTwoNumbersByScanner  
4:  {  
5:       public static void main(String[] args)  
6:        {  
7:             int no1, no2, sum;  
8:             System.out.println("Enter the two numbers ");  
9:             Scanner scan = new Scanner(System.in);  
10:            
11:            //Scan for the two numbers  
12:             no1 = scan.nextInt();  
13:             no2 = scan.nextInt();  
14:            
15:            //calculate the sum  
16:             sum = no1 + no2;  
17:             System.out.println("Sum of two numbers is : "+sum);  
18:        }  
19:  }  
20:    
21:    

Output of the program :


 Remember that you have to enter only the numbers because we are scanning for the numbers in the above program.
The above programs only enters the numbers in 4 bytes range which the size of any integer in java. If you want to add numbers more than 4 bytes you can use BigInteger class from java.math package.

The program is shown below :

1:  import java.util.Scanner;  
2:  import java.math.BigInteger;  
3:    
4:  class AddTwoNumbersBig  
5:  {  
6:       public static void main(String[] args)  
7:       {  
8:             System.out.println("Enter the two numbers ");  
9:             Scanner scan = new Scanner(System.in);  
10:             String no1 = scan.next();  
11:             String no2 = scan.next();  
12:             BigInteger first = new BigInteger(no1);  
13:             BigInteger second = new BigInteger(no2);  
14:             BigInteger sum = first.add(second);  
15:             System.out.println("Sum of two numbers is : "+sum);  
16:        }  
17:  }  
18:    

Output of the program :


8 comments :

  1. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    python Training in Bangalore | python Training in Bangalore

    ReplyDelete
  2. nice information.thank you.
    javacodegeeks.net

    ReplyDelete
  3. Great article. it is very usefull for me and my team. thanks for sharing.
    Hiring Java developer

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. 3D Iron - titanium element - TITIANART
    The babyliss pro titanium hair dryer Iron in Titanium titanium bmx frame is the basis for the famous Ring of Yggdrasil, and can titanium rings be resized is a large ring titanium trim in which ridge titanium wallet the dragon Material: Silver Rating: 4.5 · ‎9 reviews

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. CopyCat is a great Figma Plugin Development tool to quickly get started with development. It's simple to use and install, and it comes with a wide range of features that make developing for Figma much easier.

    ReplyDelete