Challenge: Average of Three
Completed
Read three whole numbers and print their average with exactly 2 decimals.
Input
Three whole numbers.
Output
Average: <value> with 2 decimals, e.g. Average: 2.33.
Example
Input
1 2 4
Output
Average: 2.33
Editor
Java
Hints
Divide by 3.0, not 3, so you don't lose the decimals.
printf("%.2f", x) prints 2 decimals.
Solution explanation
Dividing by 3.0 makes the answer a double. %.2f rounds it to 2 decimals.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
double avg = (a + b + c) / 3.0;
System.out.printf("Average: %.2f%n", avg);
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module