Dremendo Tag Line

Input decimal numbers and find their sum and average in Java

Input - Question 2

In this question, we will see how to input 5 decimal numbers in Java programming using the java input and find their sum and average. To know more about java input click on the java input lesson.

Q2) Write a program in Java to input 5 decimal numbers and find their sum and average.

Program

import java.util.Scanner;

public class Q2
{
    public static void main(String args[])
    {
        float a,b,c,d,e,sum,avg;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 5 decimal numbers");
        a=sc.nextFloat();
        b=sc.nextFloat();
        c=sc.nextFloat();
        d=sc.nextFloat();
        e=sc.nextFloat();
        sum=a+b+c+d+e;
        avg=sum/5;
        System.out.println("Sum=" + sum);
        System.out.println("Average=" + avg);
    }
}

Output

Enter 5 decimal numbers
12.36
15.1
3.24
18.9
21.17
Sum=70.77
Average=14.153999
video-poster