Answer to Question #273006 in Python for Karabo

Question #273006

The sum of the first n positive integers can be computed by the formula

sum(1..n) = 1 + 2 + 3 + 4 + · · · + n = n(n + 1)/2

Write a short Python program that computes the sum of the first 100 positive integers

and prints it to the screen, using format specifiers when printing instead of converting

each item to a string. Use variables to represent the 1, the 100, and the result of the

computation. Your program must compute the 5050 value. You cannot just print the

result to the screen. You must compute it first from the 100. The goal is for the output to

look exactly the same.The output is as shown below:

sum(1..100) = 5050



1
Expert's answer
2021-11-29T06:52:19-0500
def sumOfNums(num):
  return num * (num + 1) / 2


def display():
  num = 100
  print("sum(1..100)=%d" % sumOfNums(num))
display()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS