Skip to content
Programs · Program 7-4

Q.Program 7-4: Write a program using a user defined function that displays sum of first n natural numbers, where n is passed as an argument.

Yanam BieapTextbookSubjective· 3mImportance★★★★★est
10% · 4/40 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Program 7-4 passes n as an argument to a user-defined function that accumulates the sum 1 + 2 + … + n in a loop and displays it — for n = 10 the sum is 55.

The idea. This program introduces parameters: the caller supplies n in the call sumN(10), and inside the function n receives that value. The body uses the standard accumulator pattern — start total at 0 and add each number from 1 to n.

# Program 7-4: sum of first n natural numbers, n passed as an argument

def sumN(n):
    total = 0
    for i in range(1, n + 1):    # 1, 2, ..., n  (n+1 is excluded)
        total = total + i
    print("Sum of first", n, "natural numbers is", total)

sumN(10)

Dry run for n = 10 (state of total after each iteration):

itotal
11
23
36
410
515
621
728
836
945
1055

Expected output

Sum of first 10 natural numbers is 55
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.