Average of 3 numbers (included number rounding)

This Java code example demonstrates how to calculate the average of three numbers and round the result to a specified number of decimal places. The script includes a method for rounding double values and uses basic arithmetic operations to compute the average. It then prints the average along with its rounded values in different formats.

Code Example

``````class AvgNumbers {
private static double roundDouble(double value, int decimalPoints) {
double d = Math.pow(10, decimalPoints);
return Math.round(value * d) / d;
}

public static void main(String[] args) {
// declare variables
double a, b, c, avg;

// initialize variables
a = 11.54;
b = 7.73;
c = 10.54;

avg = (a + b + c) / 3;

System.out.println("Average of " + a + ", " + b + " and " + c + " is: " + avg);
System.out.println("Rounded value:\n"
+ String.format("%1.2f", avg) + "\n"
+ Math.round(avg) + "\n"
+ roundDouble(avg, 2) + "\n");
}
}``````

Code Explanation

Method to Round Double Values

`private static double roundDouble(double value, int decimalPoints)`

This method rounds a given double value to a specified number of decimal places.

``````private static double roundDouble(double value, int decimalPoints) {
double d = Math.pow(10, decimalPoints);
return Math.round(value * d) / d;
}``````
• Parameters:
• `value`: The double value to be rounded.
• `decimalPoints`: The number of decimal places to round to.
• Rounding Logic: The method multiplies the value by `10` raised to the power of `decimalPoints`, rounds the result, and then divides by the same power of `10` to get the rounded value.

Main Method

`public static void main(String[] args)`

The main method executes the program.

``````public static void main(String[] args) {
// declare variables
double a, b, c, avg;

// initialize variables
a = 11.54;
b = 7.73;
c = 10.54;

avg = (a + b + c) / 3;

System.out.println("Average of " + a + ", " + b + " and " + c + " is: " + avg);
System.out.println("Rounded value:\n"
+ String.format("%1.2f", avg) + "\n"
+ Math.round(avg) + "\n"
+ roundDouble(avg, 2) + "\n");
}``````
• Variable Declaration:
• `double a, b, c, avg;` declares the variables to hold the three numbers and their average.
• Variable Initialization:
• `a = 11.54;`, `b = 7.73;`, `c = 10.54;` initialize the variables with specified values.
• Average Calculation:
• `avg = (a + b + c) / 3;` calculates the average of the three numbers.
• Output Statements:
• `System.out.println("Average of " + a + ", " + b + " and " + c + " is: " + avg);` prints the calculated average.
• `System.out.println("Rounded value:\n" + String.format("%1.2f", avg) + "\n" + Math.round(avg) + "\n" + roundDouble(avg, 2) + "\n");` prints the average in three different rounded formats:
• `String.format("%1.2f", avg)` rounds the average to 2 decimal places using `String.format`.
• `Math.round(avg)` rounds the average to the nearest whole number using `Math.round`.
• `roundDouble(avg, 2)` uses the custom `roundDouble` method to round the average to 2 decimal places.