0% found this document useful (0 votes)
7 views

Python for & While Loops Programs

Uploaded by

Arnav Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python for & While Loops Programs

Uploaded by

Arnav Pawar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DELHI PUBLIC SCHOOL-BOPAL, AHMEDABAD

Subject: AI CLASS: IX
PYTHON PROGRAMS USING FOR & WHILE loops

#1. program to find reverse of a number


n=int(input("enter a number: "))
nn=n
rev=0
while n != 0:
digit=n % 10
rev=rev*10+digit
n//=10
print("Reverse of ", nn, " is ", rev)

#2. program to decide whether the given number is prime or not


n=int(input("Enter a number greater than 1: "))
c=2
while c<n:
if n % c ==0:
print(n, " is not a prime number")
break
c+=1
else:
print(n, " is a prime number")

#3. program to print common factors of any 2 numbers n1 and n2


n1=int(input("Enter first numebr :"))
n2=int(input("Enter second numebr :"))
i=1
while i < min(n1,n2):
if n1 % i ==0 and n2 % i==0:
print(i)
i+=1

#4. program to create patterns


for i in range(5):
print("*" * (i+1))
for i in range(5):
print("*" * (5-i))
#5. program to print all factors of a number
n=int(input("Enter a number: "))
for i in range(1, n+1):
if n% i ==0:
print(i, end=(","))

#6. program to find HCF & LCM of two numbers


n=int(input("Enter first number: "))
m=int(input("Enter second number: "))
hcf=1
for c in range(1,min(m,n)+1):
if m%c==0 and n%c==0:
hcf=c
lcm=m*n/hcf
print("HCF=",hcf)
print("LCM=",lcm )

#7. program to find facotial of a number


n=int(input("Enter a number: "))
fact=1
for i in range(n):
fact=fact*(i+1)
print("Factorial of ", n, " is ", fact)

#8. program to find sum of first n numbers


n=int(input("Enter a number: "))
sum=0
for i in range(n):
sum=sum+(i+1)
print("Sum of first ", n, " numbers is ", sum)

#9. program to find sum of digits of a number


n=int(input("enter a number: "))
nn=n
sum=0
while n != 0:
digit=n % 10
sum=sum+digit
n//=10
print("Sum of digits of ", nn, " is ", sum)

You might also like