Write a program to show the sum of
(1**2) + (2**2) - (3**2) + (4**2)-,...,+(n**2)
program code in python using "for" and "While" loop.
虽然我只为如下代码所示的+
迭代编写,但是+,-,+,-
迭代非常困难。这是我的+
迭代代码:
nstart = 1
nend = 4
count = 0
Sum = 0
for i in range(nstart,nend+1):
count+=1
d = i**2
Sum = Sum + d
print(count,'**2 = ', d )
print('Sum = ', Sum)
#This program print same as [(1**2,+ 2**2,+ 3**2= 9,+,,,,+n**2 and sum of them)]
您可以执行
% 2
检查以检查奇数,并相应地将符号设置为+
或-
:对于
n
值4,输出12。你知道吗另一种选择:使用
sign
变量并相应地更改它。你知道吗相关问题 更多 >
编程相关推荐