0% found this document useful (0 votes)
5 views3 pages

Python Looping Solutions

The document contains Python code snippets for various mathematical computations and patterns. It includes functions to compute powers and factorials, find GCD and LCM of three numbers, calculate series sums, identify prime numbers, print multiplication tables, generate perfect numbers, produce Fibonacci series, create star patterns, and find Armstrong numbers within a specified range. Each section is self-contained and prompts the user for input to perform the calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Python Looping Solutions

The document contains Python code snippets for various mathematical computations and patterns. It includes functions to compute powers and factorials, find GCD and LCM of three numbers, calculate series sums, identify prime numbers, print multiplication tables, generate perfect numbers, produce Fibonacci series, create star patterns, and find Armstrong numbers within a specified range. Each section is self-contained and prompts the user for input to perform the calculations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Compute x^n and n!

x = int(input("Enter x: "))
n = int(input("Enter n: "))
power = x ** n
fact = 1
for i in range(1, n+1):
fact *= i
print("x^n =", power)
print("n! =", fact)

2. GCD and LCM of 3 numbers


import math
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
g = [Link]([Link](a,b),c)
l = abs(a*b*c)//g
print("GCD =", g)
print("LCM =", l)

3. Series Sums
# (i)
s=0
for i in range(3,1692,8):
s += i
print(s)

# (ii)
s=0
for i in range(100):
s += 7 + i*13
print(s)

# (iii)
s=0
sign = 1
for i in range(75):
s += sign * (5 + i*6)
sign *= -1
print(s)

# (iv)
n = int(input("Enter n: "))
s=0
for i in range(1,n+1):
s += i*(i+1)//2
print(s)

# (v)
p=1
for i in range(2,38,5):
p *= i
print(p)

5. Prime numbers in range


a = int(input("Enter a: "))
b = int(input("Enter b: "))
for n in range(a,b+1):
if n > 1:
for i in range(2,n):
if n%i==0:
break
else:
print(n)

6. Table Pattern
n = int(input("Enter n: "))
for i in range(1,n+1):
for j in range(1,n+1):
print(i*j, end=" ")
print()

7. First n Perfect Numbers


n = int(input("Enter n: "))
count = 0
num = 1
while count < n:
s=0
for i in range(1,num):
if num%i==0:
s += i
if s == num:
print(num)
count += 1
num += 1

8. Fibonacci Series
n = int(input("Enter n: "))
a,b = 0,1
for i in range(n):
print(a, end=" ")
a,b = b,a+b

9. Star Pattern
n = int(input("Enter n: "))
for i in range(1,n+1):
print("*"*i)

10. Armstrong Numbers (1 to 10000)


for n in range(1,10001):
s=0
temp = n
while temp>0:
d = temp%10
s += d**3
temp//=10
if s == n:
print(n)

You might also like