Average of given numbers in Bash
Program to compute average in bash.
Examples:
Input : 1 2 3 4 5 Output : average : 3.0
Input : 1 2 3 4 10 Output : average : 5.0
Program to compute the average is simple. It is also called Mean.
Formula:
(sum of all elements) / (total no. of elements)
The extension for bash programs end with .sh. Elements are stored in array which are traversed in while loop to calculate the sum. It is recommended to understand Arrays in Shell.
The avg is calculated using bc command. bc command is used for command line calculator.
Programs in bash is executed as follows:
sh program_name.sh
OR
./program_name.shExample:
C++
# Total numbersn=5# copying the value of nm=$n# initialized sum by 0sum=0# array initialized with# some numbersarray=(1 2 3 4 5)# loop until n is greater# than 0while [ $n -gt 0 ]do # copy element in a # temp variable num=${array[`expr $n - 1`]} # add them to sum sum=`expr $sum + $num` # decrement count of n n=`expr $n - 1`done# displaying the average# by piping with bc command# bc is bash calculator# commandavg=`echo "$sum / $m" | bc -l`printf '%0.3f' "$avg" |
Java
import java.math.BigDecimal;public class Main { public static void main(String[] args) { // Total numbers int n = 5; // copying the value of n int m = n; // initialized sum by 0 int sum = 0; // array initialized with some numbers int[] array = {1, 2, 3, 4, 5}; // loop until n is greater than 0 while (n > 0) { // copy element in a temp variable int num = array[n - 1]; // add them to sum sum += num; // decrement count of n n--; } // displaying the average // by using BigDecimal to perform precise division BigDecimal avg = new BigDecimal((double) sum / m); System.out.printf("%.3f", avg); }} |
Javascript
// Total numberslet n = 5;// copying the value of nlet m = n;// initialized sum by 0let sum = 0;// array initialized with some numberslet array = [1, 2, 3, 4, 5];// loop until n is greater than 0while (n > 0) { // copy element in a temp variable let num = array[n - 1]; // add them to sum sum += num; // decrement count of n n--;}// displaying the averagelet avg = sum / m;console.log(avg.toFixed(3)); |
Python3
# Total numbersn = 5# copying the value of nm = n# initialized sum by 0sum = 0# array initialized with some numbersarray = [1, 2, 3, 4, 5]# loop until n is greater than 0while n > 0: # copy element in a temp variable num = array[n - 1] # add them to sum sum += num # decrement count of n n -= 1# displaying the averageavg = sum / mprint(format(avg, '.1f')) |
C#
using System;class Program { static void Main(string[] args) { // Total numbers int n = 5; // copying the value of n int m = n; // initialized sum by 0 int sum = 0; // array initialized with some numbers int[] array = { 1, 2, 3, 4, 5 }; // loop until n is greater than 0 while (n > 0) { // copy element in a temp variable int num = array[n - 1]; // add them to sum sum += num; // decrement count of n n--; } // displaying the average double avg = (double) sum / m; Console.WriteLine(avg.ToString("0.000")); }} |
Output :
3.0
Time complexity: O(n) where n is given total numbers
Auxiliary space: O(1)


Please Login to comment...