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

Python Programs 1

The document contains a series of Python programs that perform various mathematical operations, including finding GCD and LCM, calculating factorials, checking for prime and perfect numbers, and generating Fibonacci series. It also includes functions for swapping numbers, checking even/odd status, counting digits, reversing numbers, and printing multiplication tables. Each program is presented with input prompts and output statements.
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)
3 views5 pages

Python Programs 1

The document contains a series of Python programs that perform various mathematical operations, including finding GCD and LCM, calculating factorials, checking for prime and perfect numbers, and generating Fibonacci series. It also includes functions for swapping numbers, checking even/odd status, counting digits, reversing numbers, and printing multiplication tables. Each program is presented with input prompts and output statements.
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

PYTHON PROGRAMS

1. Find GCD (HCF) and LCM of two numbers

n1=int(input("ENTER A NUMBER"))
n2=int(input("ENTER ANOTHER NUMBER"))

if n1< n2:
small = n1
else:
small = n2
gcd =1
for i in range(1,small+1):
if n1 % I == 0 and n2 % I == 0:
gcd = gcd*i
print("gcd=",gcd)

lcm=(n1*n2)/gcd
print("lcm=",lcm)

2. Find the factorial of given number

n=int(input("ENTERANUMBER"))

fact=1
for i in range(1,n+1):
fact = fact*i
print("factorial of ",n," =",fact)

3. Check if a given number is Prime number or not

n=int(input("ENTERANUMBER"))

count=0
for i in range(2,n):
if n%i == 0:
count=count+1
break
if count == 0:
print(n,"is a prime number")
else:
print(n,"is not a prime number")
4. Print all Prime Numbers in a range

lower =int(input("Please,EntertheLowestRangeValue:"))

upper= int(input ("Please,Enterthe UpperRange Value: "))

print("ThePrimeNumbersintherangeare:")

for number in range (lower, upper+1):


count=0
for i in range(2,number):
if number%i ==0:
count=count+1
break
if count==0:
print(number,end="")

5. To check if a number is Perfect number or not

n=int(input("Enterthenumber:"))

sum=0
for i in range(1,n):
if n%i==0:
sum=sum+i

ifsum == n:
print("itisaperfectnumber")
else:
print("Itisnota perfectnumber")
6. SWAPPING NUMBERS

A=5 a=5
B=7 b=7
Print(“before swapping”) Print(“before swapping”)
Print(“a=”,a) Print(“a=”,a)
Print(“b=”,b) Print(“b=”,b)

C=a a=a+b
a=b b=a-b
b=c a=a-b

Print(“after swapping”) Print(“after swapping”)


Print(“a=”,a) Print(“a=”,a)
Print(“b=”,b) Print(“b=”,b)

7. To check if a number is even / odd

n=int(input("Enterthenumber:"))

if n % 2 == 0:

print(“even number”)
else:
print(“odd number”)

8. PRINT ALL EVEN NUMBERS IN A RANGE

lower =int(input("Please,EntertheLowestRangeValue:"))

upper= int(input ("Please,Enterthe UpperRange Value: "))

print("ThePrimeNumbersintherangeare:")

for number in range (lower, upper+1):


if number % 2 ==0:
print(number,end=””)
9. TO COUNT DIGITS OF A NUMBER

n=int(input("Enterthenumber:"))

count = 0

while n>0:
digit = n%10
count=count+1
n=n//10
print("total number of digits are : ",count)

10. To REVERSE A NUMBER

n=int(input("Enterthenumber:"))

temp=n
reverse=0

while n>0:
remainder=n%10
reverse=reverse*10+remainder
n=n//10
print("reverse=",reverse)

11. To check a number is palindrome or not

n=int(input("Enterthenumber:"))
temp=n
reverse=0
while n>0:
remainder=n%10
reverse=reverse*10+remainder
n=n//10

print("reverse=",reverse)
if temp==reverse:
print("it isapalindrome number")
else:
print("it isnotapalindromenumber")
12. TO PRINT MULTIPLICATION TABLE
n=int(input("Enter the number:"))
term=int(input("Enter the last term"))

print("Multiplication table of", n)

for i in range(1,term+1):
mult=n*i
print(n, “ x ”, i, “=” , mult)

13. To check if a given number is Armstrong number

n=int(input("Enterthenumber:"))

temp=n
sum =0
whilen>0:
rem=n%10
sum=sum+rem**3
n=n//10

if temp == sum:
print("it is anarmstrong number")
else:
print("it is not an Armstrong number")

14. TO GENERATE FIBONACCI SERIES

term=int(input("Enter the last term"))


a=-1
b=1

for i in range(1,term+1):
sum=a+b
print(sum,end=” “)
a=b
b=sum

You might also like