Computer Science, asked by paryusha4990, 7 months ago

Algorithm to calculate the average of twelve numbers

Answers

Answered by Pablu
0

1st thought, can I simplify? (And further down the road make an abstraction to include an algorithm to find any sum from “X” to “Y”?)

There are multiple ways to simplify the problem, one way is you could start off by lowering your end number (12) to a lower number., whether increasing it from 1 to 12.

Step 1: Set a SUM variable to 0

Step 2: Start from x = 1

Step 3: Keep moving untill it is less than equals to 12 or less than 13

Step 4: Increment the operator

Step 5: Just do SUM += x, that will calculate the sum from 1 to 12

Initially SUM = 0, when it enters the loop it adds with 1 ; 0+1 = 1, then 1 adds with 2; 1+2 = 3 and so on... upto 12

Step 6: Find AVERAGE by dividing the SUM by number of digits which is 12

----> Let’s try it with the simplest way (This is some type of psuedo-code, for legibility):

// Python3 Code

def avgNum(n) :

  sum = 0

   for i in range(n):

       sum += i

       i++

  avg = sum/n

print(" The Average of ", n, "numbers is ", avg)

if __name__ == "__main__" :

say = int(input(" Enter any number :")) // Type 12 in console

avgNum(say)

// Kindly check for the proper Identation because the code is written in Python programming language.

Hope you got it !

Similar questions