In this tutorial, we are going to learn a writing java program to find the average of numbers given as input from the user. This program is little bit different from the previous Average program, Where we have calculated the average of Integer array in java.
Problem Statement
For any sequence of input numbers that are given by the user, we have to calculate the average of the input numbers using java program.
For example
Case1: If the Input sequence given by user are 1,2,3,4,5.
The output should be 3.
Case2: If the Input Sequence given by user are 11,22,33,44,55,66.
The output should be 38.5.
What is the Average of the Numbers?
Average also called the arithmetic mean, is calculated by adding a group or collection of numbers and then dividing that sum by the number of elements of the same group or collection.
Example
For given numbers 10, 20, 30, 40, 50 Output should be : 30 (After calculating average)
Algorithm to Calculate Average of the Numbers
Step 1: Start
Step 2: float sum =0;
Step 3: Scanner sc = new Scanner(System.in);
Step 4: input array size int n= sc.nextInt();
Step 5: create an empty array of size n.
Step 6: for(int i=0; i < n; i++) { arr[i] = sc.nextInt(); sum=sum+arr[i]; }
Step 7: assign a variable, avg as float average = sum/n;
Step 8: print average
Step 9: Stop
Our Logic to calculate the Average of Number Given by users
- Our program will have declare the variable sum =0.
- Also our program will take an integer input from user as the size of array.
- Then In the empty array, elements of the array will be taken from the user using ‘For’ loop.
- Then. Our program will calculate the sum of all the numbers using ‘for loop’.
- Finally, the calculated sum is then divided by the size of array and print the result as the output.
- And Our final output will be the average of the number given by the user.
Java Program to Calculate Average of Given Input Integer
Program 1: Average from an Array of Integers
import java.util.*;
public class FindAverage
{
public static void main(String[] args) {
float sum=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array: ");
int n = sc.nextInt();
float arr[] = new float[n];
System.out.println("Enter " + n + " array elements: ");
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
sum=sum+arr[i];
}
float average = sum/n;
System.out.println("Average of number is : "+average);
}
}
Output
Enter the size of array: 5 Enter 5 array elements: 10 20 30 40 50 Average of number is : 30.0
Explanation
In this example, the input array is {1, 2, 3, 4, 6}. Where the size of array is 5 so the average will be
Average = (1+2+3+4+6)/ 5 , The output generated is 3.2.
Program 2: Average of User Input via Console
This approach prompts the user to enter numbers one by one in the console, and calculates the average.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number for input elements: ");
int n = scanner.nextInt();
double sum = 0;
for (int i = 0; i < n; i++) {
System.out.print("Enter number " + (i + 1) + ": ");
sum += scanner.nextInt();
}
scanner.close();
System.out.printf("The average is: %.2f%n", sum / n);
}
}
Output
Enter the number for input elements: 5 Enter number 1: 23 Enter number 2: 12 Enter number 3: 33 Enter number 4: 10 Enter number 5: 11 Average of input number is: 17.80
In the above program we have seen writing java program to find the average of given integer inputs. Hope this post is helpful in learning this programs.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…