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

Python Programs Using If and Loops

The document discusses Python programs using conditional statements and loops. It provides 10 examples of programs that check conditions like even/odd, leap year, greater than a number, uppercase/lowercase. It also provides examples of programs using loops like factorial, Armstrong number, reverse, sum of digits, Fibonacci series, palindrome, prime number check, individual digits print, even numbers print, and multiplication table print.

Uploaded by

Kavin Kavin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views5 pages

Python Programs Using If and Loops

The document discusses Python programs using conditional statements and loops. It provides 10 examples of programs that check conditions like even/odd, leap year, greater than a number, uppercase/lowercase. It also provides examples of programs using loops like factorial, Armstrong number, reverse, sum of digits, Fibonacci series, palindrome, prime number check, individual digits print, even numbers print, and multiplication table print.

Uploaded by

Kavin Kavin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

3. Python Programs using if..

else
1. Check a number is even or odd

# program for even or odd


n = int(input("enter a number"))

if (n%2==0):
print(n, "is even number")
else:
print(n, "is odd number ")

2. Year is leap year or not

# program for leap year or not


n = int(input("enter a year value"))

if (n%4==0):
print(n, "is leap year")
else:
print(n, "is not leap year")

3. Check whether the given number is greater than5, if greater than 5, print
the cube of the number, if less than print the square of the number

a= int(input("enter a number"))
if (a>5):
print("cube of ", a, "is", a*a*a)
else:
print("square of", a, "is", a*a)

4. Check whether the given character is upper case


a = input("enter a character")
if (a.islower()):
print(" the given character", a, "is lower case letter")
else:
print(" the given character", a, "is upper case letter")

5. Largest of three numbers


# Python program to find the largest number among the three input numbers

num1 = int(input(“enter the first number)


num2 = int(input(“enter the second number)
num3 = int(input(“enter the third number)

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

4. Python Programs using loops


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

print("factorial of ", n, " is", fact)

2. Find whether the given number is Armstrong or not

#armstrong or not
a = int(input("enter a number"))
s=0
t=a
while(a >0):
n = a%10
s=s+n*n*n
a=a//10

if(s==t):
print("the given number is armstrong number", t)
else:
print("the given number is not armstrong number", t)

3. Reverse of a number
#reverse of a number
n = int(input("enter a number"))

n1 = n
rev =0
while(n >0):
digi = n%10
rev=rev*10+digi
n = n//10
print(" the reverse of ",n1, " is ", rev)

4. Sum of individual digits of a number

#sum of digits
n = int(input("enter a number"))

n1 = n
sum =0
while(n >0):
digi = n%10
sum=sum+digi
n = n//10
print(" the sum of digits in ",n1, " is ", sum)

5. Fibonacci series

#fibonacci series
n=int(input("enter a number"))
f1=0
f2=1
i=1
print(f1)
print(f2)
while(i<=n):
f3=f1+f2
print(f3)
f1=f2
f2=f3
i=i+1
print("**********")

6. Check whether a number is Palindrome or not

n = int(input("enter a number"))

n1 = n
rev =0
while(n >0):
digi = n%10
rev=rev*10+digi
n = n//10
print(" the reverse of ",n1, " is ", rev)

if(n1==rev):
print(n1, “is palindrome”)
else:
print(n1, “is not palindrome”)
7. Find whether a number is prime or not

# Program to check if a number is prime or not


# To take input from the user
num = int(input("Enter a number: "))

# define a flag variable


flag = False

if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True


if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
8. Print the individual digits of a number
n=int(input("enter a number"))
s=str(n)
for i in s:
print(i)

9. Print the even numbers from 2 to n

n = int(input("enter a number"))
for i in range(2,n+1, 2):
print(i)

10. Print multiplication table

# multiplication table
n = int(input("enter a number"))
for i in range(1,13):
print(i,"X",n, "=", i*n)

You might also like