Write a python program to Implement Addition of 10 Natural Numbers
Description
This Python code utilizes a while loop to print numbers from 1 to 10. It initializes a variable 'i' to 1 and uses the while loop with the condition i <= 10
to print each number, incrementing 'i' by 1 in each iteration until it reaches 10.
Code
program4b.py
i=1
while(i<=10):
print(i)
i=i+1
Explanation of above code
- The Python code is a simple program that utilizes a 'while' loop to print numbers ranging from 1 to 10.
- It begins by initializing a variable 'i' with the value 1, serving as the starting point for the sequence of numbers to be printed.
- The 'while' loop condition,
i <= 10,
ensures that the loop continues executing as long as the value of 'i' is less than or equal to 10. - Within the loop, the code prints the current value of 'i' using the 'print' statement.
- The code then increments 'i' by 1 in each iteration using 'i = i + 1,' ensuring that the loop eventually terminates when 'i' becomes greater than 10.
- Consequently, upon running the code, it will print the numbers from 1 to 10, with each number appearing on a new line. This code offers a straightforward approach to generating a sequence of numbers within a specified range using a 'while' loop.