0

Age average

Why I can’t get correct result , 28.4? public class Program { public static void main(String[] args) { int grace = 10; int edward = 21; int rose = 15; int mom = 46; int dad = 50; float average = (grace+edward+rose+mom+dad)/5; System.out.println(average); } }

25th Oct 2019, 12:11 AM
Mali
3 Answers
+ 1
You defined the numbers as integers, so when they are divided, they are divided as integers, rounding the decimal to whole number. If you want to keep the decimal, try: //my code is in C#, adapt it to Java using System; public class Program { public static void Main(String[] args) { float grace = 10; float edward = 21; float rose = 15; float mom = 46; float dad = 50; float average = (grace+edward+rose+mom+dad)/5; Console.WriteLine(average); } }
25th Oct 2019, 1:00 AM
Rodrigo Oliveira
Rodrigo Oliveira - avatar
+ 1
What does this output?
25th Oct 2019, 12:16 AM
Álvaro Rodríguez García
Álvaro Rodríguez García - avatar
0
24.0
25th Oct 2019, 12:21 AM
Mali