Tuesday, August 15, 2017

Sum of Numbers in Java

Sum of Numbers in Java

In this section, we will create Java programs

to find the sum or addition of two numbers using the method and command-line arguments

, the sum of three numbers, and the sum of n numbers.

Sum of Two Numbers in Java

In Java, finding the sum of two or more numbers is very easy. First, declare and initialize two variables to be added. Another variable to store the sum of numbers. Apply mathematical operator (+) between the declared variable and store the result. The following program calculates and prints the sum of two numbers.

SumOfNumbers1.java

  1. public class SumOfNumbers1  
  2. {  
  3. public static void main(String args[])   
  4. {  
  5. int n1 = 225, n2 = 115, sum;  
  6. sum = n1 + n2;  
  7. System.out.println("The sum of numbers is: "+sum);  
  8. }  
  9. }  

Output:

The sum of numbers is: 340

Sum of Two Numbers in Java Using Method

There are two ways to find the sum of two numbers in Java.

  • By using User-defined Method
  • By using sum() Method

By Using User-defined Method

The Java Scanner class

allows us to read input from the user. We take two numbers as input and pass them to the user-defined method sum(). The following program calculates the sum of two numbers using the method and prints the result.

SumOfNumbers2.java

  1. import java.util.Scanner;  
  2. public class SumOfNumbers2  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int x, y, sum;  
  7. Scanner sc = new Scanner(System.in);  
  8. System.out.print("Enter the first number: ");  
  9. x = sc.nextInt();  
  10. System.out.print("Enter the second number: ");  
  11. y = sc.nextInt();  
  12. sum = sum(x, y);  
  13. System.out.println("The sum of two numbers x and y is: " + sum);  
  14. }  
  15. //method that calculates the sum  
  16. public static int sum(int a, int b)  
  17. {  
  18. int sum = a + b;  
  19. return sum;  
  20. }  
  21. }  

Output:

Enter the first number: 34
Enter the second number: 12
The sum of two numbers x and y is: 46

By Using Integer.sum() Method

The Integer class

provides the sum() method. It is a static method that adds two integers together as per the + operator. It can be overloaded and accepts the arguments in intdouble

float

, and long

.

Syntax:

  1. public static int sum(int a, int b)  

It returns the sum of the numbers that are passed as an argument. It throws ArithmaticException when the result overflows an integer value.

Note: If both arguments are negative, the result will be negative.

SumOfNumbers3.java

  1. public class SumOfNumbers3  
  2. {    
  3. public static void main(String args[])   
  4. {            
  5. int x = 33, y = 55, a = -15, b = -23, sum, s;    
  6. sum=Integer.sum(x, y);  
  7. //returns the sum of x and y  
  8. System.out.println("The sum of x and y is: " +sum);    
  9. s=Integer.sum(a, b);  
  10. System.out.println("The sum of x and y is: " +s);  
  11. }    
  12. }   

Output:

The sum of x and y is: 88
The sum of x and y is: -38

Similarly, we can calculate the sum of double, float, and long type numbers.

Sum of Two Numbers Using Command Line Arguments in Java

The command-line arguments are passed to the program at run-time. Passing command-line arguments in a Java program is quite easy. They are stored as strings in the String array

passed to the args[] parameter of the main() method in Java.

SumOfNumbers4.java

  1. public class SumOfNumbers4  
  2. {  
  3. public static void main(String args[])  
  4. {  
  5. int x = Integer.parseInt(args[0]); //first arguments   
  6. int y = Integer.parseInt(args[1]); //second arguments  
  7. int sum = x + y;  
  8. System.out.println("The sum of x and y is: " +sum);  
  9. }  
  10. }  

Output:

The sum of x and y is: 101

First, compile the above program by using the command javac SumOfNumbers4.java. After that, run the program by using the command java SumOfNumbers4 89 12. Where 89 and 12 are command-line arguments.

Sum of Three Numbers in Java

The program for the sum of three numbers is the same as the sum of two numbers except there are three variables.

SumOfNumbers5.java

  1. import java.util.Scanner;  
  2. public class SumOfNumbers5  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int x, y, z, sum;  
  7. Scanner sc = new Scanner(System.in);  
  8. System.out.print("Enter the first number: ");  
  9. x = sc.nextInt();  
  10. System.out.print("Enter the second number: ");  
  11. y = sc.nextInt();  
  12. System.out.print("Enter the third number: ");  
  13. z = sc.nextInt();  
  14. sum = sum(x, y, z);  
  15. System.out.println("The sum of three numbers x, y, and z is: " + sum);  
  16. }  
  17. public static int sum(int a, int b, int c)  
  18. {  
  19. int sum = a + b + c;  
  20. return sum;  
  21. }  
  22. }  

Output:

Enter the first number: 12
Enter the second number: 34
Enter the third number: 99
The sum of three numbers x, y, and z is: 145

Sum of N Numbers in Java

  1. Read or initialize an integer N (number of integers to add).
  2. Run a loop up to N times that ask to provide integers (to be added) again and again.
  3. Calculate the cumulative sum for each input and store it into a variable (sum).
  4. After terminating the loop, print the result.

Let's implement the above steps in a Java program.

SumOfNumbers6.java

  1. import java.util.*;  
  2. public class SumOfNumbers6  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int i, sum=0, z;  
  7. Scanner sc = new Scanner(System.in);  
  8. System.out.print("\n Enter Number of Numbers to be Calculated: ");  
  9. int n = sc.nextInt();  
  10. for(i=0;i<n;i++)   //loop executes n times  
  11. {  
  12. System.out.print("Enter the number: ");  
  13. z = sc.nextInt(); //reads integer from the user  
  14. sum=sum+z;       //calculates sum  
  15. }  
  16. System.out.println("The sum of the numbers is: "+sum);  
  17. }  
  18. }  

Output:

Enter Number of Numbers to be Calculated: 4
Enter the number: 12
Enter the number: 13
Enter the number: 5
Enter the number: 4
The sum of the numbers is: 34

 ..

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...