Java

# How to Get the Sum and Average of Two Numbers in Java

In Java, finding the sum and average of two or more numbers is straightforward. Average values are utilized to simplify a vast amount of data into a single or unique value. It is a visual and 3D depiction of the whole numbers in the data set’s which are accessible. While calculating the average of numbers, first sum all numbers and then divide by the total number of data points.

This post explains:

## How to Get/Calculate the Sum of Two Numbers in Java?

To get the sum of two numbers in Java, you can utilize the addition operation “+”. For practical implications, try out the stated procedure listed below:

• First, declare and initialize the variables that need to be added.
• Then, initialize another variable for storing the resultant sum.
• Lastly, print the result with the help of the “println()” method and add the variable name inside the “()”:
int number1 = 15;
int number2 = 17;
int number3 = 12;
int number4 = 26;
int sum1 = number1+ number2;
int sum2 = number3 + number4;
System.out.println(sum1);
System.out.println(sum2);

It can be observed that the specified numbers have been added successfully in Java:

## How to Get/Calculate the Average of Two Numbers in Java?

To get the average of two numbers in Java, follow the given instructions:

• First, initialize the variable along with the data type and declare the value of each variable.
• Next, initialize another variable with the float data type. Then, utilize the formula of average “Sum of the values/Total number of values”.
• Lastly, print the output of the average with the help of the “System.out.println()” method:
int number1 = 15;
int number2 = 17;
int number3 = 12;
int number4 = 26;
float avg1 = (float)((number1 + number2) / 2);
float avg2 = (float)((number3+ number4) / 2);
System.out.println(avg1);
System.out.println(avg2);

As you can see, the average of the passed arguments has been printed out in float data type:

That’s all about getting the sum and average of two numbers in Java.

## Conclusion

To get the sum and average of two numbers in Java, initialize the variable along the data type and specify the value of each variable. Then, declare another variable and utilize the formula of sum or average as a variable’s value. Lastly, use the “System.out.println()” method to display output on the console. This post explained the method for getting the sum and average of two numbers in Java.