How can I write a Python program to calculate the …

Computers and Technology Questions

Write a program in python to calculate sum of first 10 natural numbers

Short Answer

To sum the first 10 natural numbers, initialize a variable `sum` to 0 and use a for loop with `range(1, 11)` to add each number to `sum`. Finally, print the result, which should yield a total of 55.

Step-by-Step Solution

Step 1: Set Up the Program

Begin by initializing a variable called sum to store the total. This will be used to accumulate the values of the natural numbers as you loop through them. You should also consider using a for loop to iterate through the range of numbers you want to sum.

  • Set sum = 0
  • Use range(1, 11) to cover the first 10 natural numbers.

Step 2: Implement the Loop

Within the for loop, add each number to the sum variable. As the loop iterates through each value from 1 to 10, the program will continuously update the sum variable with the current number. This is achieved using the += operator.

  • For each i in range(1, 11):
  • Update the sum: sum += i

Step 3: Print the Result

After the loop has completed its iterations, the final sum (which should be 55) needs to be printed to display the result. This is done using the print() function. You can simply call print(sum) to show the output on the console.

  • Run the program to confirm the result.
  • The expected output should be: 55

Related Concepts

Variable

A storage location in a program identified by a name that holds data which can be changed during the execution of the program.

For Loop

A control flow statement that allows code to be executed repeatedly based on a given boolean condition, typically used to iterate over a sequence or range of values.

Print Function

A built-in function in programming languages like python used to output data to the console or standard output device.

Scroll to Top