Write a Java program to find sum and average of three
numbers.
Answers
Required program:
import java.util.*;
public class Numbers
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int a,b,c;
int sum;
double avg;
System.out.println("Enter 1 number: ");
a=sc.nextInt();
System.out.println("Enter 2 number: ");
b=sc.nextInt();
System.out.println("Enter 3 number: ");
c=sc.nextInt();
sum = a + b + c;
System.out.println(" Sum of three numbers is:" + sum);
avg = sum/3;
System.out.println(" Average of three numbers is:" + avg);
}
}
Input:
Enter 1 number:
45
Enter 2 number:
34
Enter 3 number:
67
Output:
Enter 1 number:
45
Enter 2 number:
34
Enter 3 number:
67
Sum of three numbers is: 146
Average of three numbers is : 48.6666666667
Hope it helps ^_^
import java.util.*;
public class AverageCalculator {
public static void main (String [] args) {
//Taking input from the user
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter the second number: ");
double num2 = sc.nextDouble();
System.out.print("Enter the third number: ");
double num3 = sc.nextDouble();
//Main part comes here
double Average = (num1 + num2 + num3) / 3;
double sum = num1 + num2 + num3;
System.out.println("The sum of the following numbers is " + sum);
System.out.println("The average of the given numbers is " + Average);
}
}
Output:
Enter the first number: 30
Enter the second number: 31
Enter the third number: 28
The sum of the following numbers is 89.0
The average of the given numbers is 29.666666666666668