- Addition of First n Numbers
The addition of the first n numbers program is used to calculate the total sum of numbers from 1 to n. This program helps beginners understand how loops and variables work in Python. Using a while loop, the program repeatedly adds numbers until the condition becomes false. For example, if n = 5, the result will be 15.
n = 5
i = 1
total = 0
while i <= n:
total = total + i
i = i + 1
print("Sum =", total)
- Multiplication of First n Numbers
The multiplication of the first n numbers program multiplies all numbers from 1 to n. It teaches students how repeated multiplication works using loops. The result for n = 4 will be 24 because 1 × 2 × 3 × 4 = 24. In this program, the initial value starts from 1.
n = 4
i = 1
product = 1
while i <= n:
product = product * i
i = i + 1
print("Product =", product)
- Factorial of a Number
The factorial of a number means multiplying all positive integers from 1 up to that number. It is represented using !. For example, 5! = 120. Factorial programs are important in mathematics and computer science. A while loop can be used to calculate factorial easily in Python.
n = 5
i = 1
fact = 1
while i <= n:
fact = fact * i
i = i + 1
print("Factorial =", fact)
Top comments (0)