0% found this document useful (0 votes)
1 views5 pages

Python Program While Loop

Uploaded by

nearly77u
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Python Program While Loop

Uploaded by

nearly77u
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Print numbers 1 to 10

python

i=1

while i <= 10:

print(i)

i += 1

2. Sum of numbers from 1 to n

python

n = 10

total = 0

i=1

while i <= n:

total += i

i += 1

print("Sum:", total)

3. Factorial of a number

python

n=5

fact = 1

i=1

while i <= n:

fact *= i

i += 1

print("Factorial:", fact)

4. Reverse a number

python

num = 1234
reversed_num = 0

while num > 0:

digit = num % 10

reversed_num = reversed_num * 10 + digit

num //= 10

print("Reversed:", reversed_num)

5. Check if a number is a palindrome

python

num = 121

original = num

reversed_num = 0

while num > 0:

digit = num % 10

reversed_num = reversed_num * 10 + digit

num //= 10

if original == reversed_num:

print("Palindrome")

else: print("Not a palindrome")

6. Multiplication table

python

n=5

i=1

while i <= 10:

print(n, "x", i, "=", n * i)

i += 1

7. Count digits in a number

python
num = 56789

count = 0

while num > 0:

num //= 10

count += 1

print("Number of digits:", count)

8. Fibonacci series

python

n = 10

a, b = 0, 1

count = 0

while count < n:

print(a, end=" ")

a, b = b, a + b

count += 1

1. Sum of squares (1² + 2² + ... + n²)

python

n=5

i=1

total = 0

while i <= n:

total += i ** 2

i += 1

print("Sum of squares:", total)

2. Sum of cubes (1³ + 2³ + ... + n³)

python

n=5
i=1

total = 0

while i <= n:

total += i ** 3

i += 1

print("Sum of cubes:", total)

3. Even number series up to n terms

python

n = 10

i=1

count = 0

while count < n:

print(2 * i, end=" ")

i += 1

count += 1

4. Odd number series up to n terms

python

n = 10

i=1

count = 0

while count < n:

print(2 * i - 1, end=" ")

i += 1

count += 1

5. Sum of even numbers up to n

python

n = 20
i=2

total = 0

while i <= n:

total += i

i += 2

print("Sum of even numbers:", total)

10. Triangular number series (1, 3, 6, 10, 15...)

python

n=6

i=1

total = 0

while i <= n:

total += i

print(total, end=" ")

i += 1

You might also like