n = int(input ("enter the number input; ")) sumN = 0 for nCount in range(n): inputNumber = int(input("Enter your next number; ")) sumN += inputNumber average = sumN/n print "The sum of the number is; ",sumN print "The average of the number is; ",average Can anybody explain this to me?
It's hard to say without knowing what language it is in, because it depends what the 'range()' function does, but I think this is a function to calculate the mean (i.e. average) of all the numbers from 1 to the number you input. E.g. if you input 5, it would add (1+2+3+4+5) and then divide it by 5 to get 5. The average will always be the input. It also tells you the sum of the numbers from 1 to your input. (assuming range returns the numbers from 1 to n)
From what I can gather the program is does this: -Ask user to input a number (n) -Creates a loop that asks the user to input more numbers. The number of loops is determined by the first input number i.e If the first number entered is 5 the program asks for 5 more numbers. -As the numbers are entered they are added to a total. (sumN) -When the loop finishes the average is calculated using the total divided by the first number (average = sumN/n) -The program then prints the values of the total and average. What programming language is this written btw? I'm guessing Python? If so, this should explain the range() function. http://docs.python.org/release/1.5.1p1/tut/range.html
// Receives input from user, turns it into an int, and stores it in n. n = int(input ("enter the number input; ")) // Declares 'sum' variable and initializes it as zero. sumN = 0 // For every number from 1 (or 0) to n.. for nCount in range(n): // Receive number from user to add to sumN. inputNumber = int(input("Enter your next number; ")) sumN += inputNumber // Find the average of all of the numbers entered. average = sumN/n // Output information. print "The sum of the number is; ",sumN print "The average of the number is; ",average Essentially, n acts as the amount of numbers to find the average of.
Join our real-time social learning platform and learn together with your friends!