该程序是关于分别给出从 1 到给定的 n 项的奇数和偶数的总和。 输入
print()
print("Program to display sum of n terms of odd/even natural numbers!")
print()
num = int(input("Enter the number of natural numbers: "))
even_total = 0
odd_total = 0
i = 1
while i == num:
if(i % 2 == 0):
even_total = even_total + i
i += 1
else:
odd_total = odd_total + i
i += 1
print()
print("The sum of even numbers from 1 to {0} = {1}".format(i, even_total))
print("The sum of odd numbers from 1 to {0} = {1}".format(i, odd_total))
输出:
Program to display sum of n terms of odd/even natural numbers!
Enter the number of natural numbers: 10
The sum of even numbers from 1 to 1 = 0
The sum of odd numbers from 1 to 1 = 0
>>>
德玛西亚99
冉冉说
料青山看我应如是
沧海一幻觉
开满天机
相关分类