1
n = int(input('Enter n: '))
count = 1
sum = 0
number = 1
while (count <= n):
    sum = sum + number
    count = count + 1
    number = number + 2
print('Sum =', sum)

Is it possible to use the same concept for 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 .... + n

john123
  • 11
  • 2
  • There is a couple ways of doing this check out this SO question: https://stackoverflow.com/q/39560167/2175781 –  Mar 24 '18 at 02:48
  • This question was asked a few hours ago: https://stackoverflow.com/questions/49460128/write-a-program-to-compute-the-sum-of-the-terms-of-the-series/49460245?noredirect=1#comment85925468_49460245 Is that your account that asked the other? – Olivier Melançon Mar 24 '18 at 05:23

2 Answers2

1

You could use a list comprehension to make this more elegant and pythonic:

def sum_series(start, end):
  return sum([i**2 for i in range(start, end+1)])

print(sum_series(1,10))

Output:

385

Or using higher order functions:

>>> sum(map(lambda x: x**2, range(1,11)))
385
user3483203
  • 50,081
  • 9
  • 65
  • 94
0

Something like this should do

n = int(input('Enter n: '))
count = 1
sum = 0
while (count <= n):
  sum = sum + count*count
  print("{s}+".format(s=sum)      
  count = count + 1
print('Sum =', sum)
srp
  • 560
  • 1
  • 5
  • 14
  • if n = 100, I want the program to run 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 and displays the result 385. Is it possible to restrict n so it can stop looping – john123 Mar 24 '18 at 03:05
  • @john n=10 then only it will be 100. i have modified my answer – srp Mar 24 '18 at 03:12