1.
Write a program to test if a given number is positive, negative or zero using if-elif-else
num=int(input("Enter a number: "))
if num>0:
print(num,"is a positive number! ")
elif num<0:
print(num,"is a negative number!")
else:
print("You have entered ZERO!")
2. Write a program to input a year and check if it is a leap year or not.
year=int(input("Enter the year you want to check! : "))
if (year%4==0)or(year%100 and year%400==0):
print("You have entered a leap year")
else:
print(year,"is not a leap year")
3. Write a program to input age from user and print if as per the input-age, one is eligible to vote
or not.
age=int(input("Enter your age : "))
if age>=18:
print("Great! you are eligible for voting!")
else:
print("You are not eligible for voting")
4. Write a program to input an integer and print two of its adjacent numbers, e.g., if you input 10,
then print 11 and 12.
num=int(input("Enter the number : "))
print("The adjacent numbers of",num,"are :", num-1,"&",num+1)
5. Write a program to the input base and height of a triangle and print its area as (½) x
base x height.
base=float(input("Enter the value of Base :"))
height=float(input("Enter the value of Height :"))
area_of_triangle=(1/2)*height*base
print("The area of triangle is :",area_of_triangle)
6. Write a program to input age in years and print in months.
age_in_yrs=int(input("Enter your age in years:"))
age_in_months=age_in_yrs*12
print("Your age in months :",age_in_months)
7. Write a program to swap two numbers using a third variable.
x=int(input("Enter first number x :"))
y=int(input("Enter second number y :"))
z=x
x=y
y=z
print("The numbers after swapping are-->",end=" ")
print("x :",x," ","y :",y)
8. To print the multiplication table of 5 using the while loop.
i=1
while i<=10:
print(5," x ",i," = ",i*5)
i=i+1
9. Write a program to combine the elements of the two lists.
list1=[10,20,30,40]
list2=[100,200,300,400]
combine=list1+list2
print("Combined list:",combine)
[Link] print odd numbers from 1 to 50.
print("Odd numbers from 1 to 50 are -->")
for x in range(1,51):
if (x%2!=0):
print(x,end=", ")
[Link] print the sum of first 15 natural numbers.
sum=0
for x in range(1,16):
sum+=x
print("Sum of first 15 natural numbers is --> ",sum)
12. Write a Python code to take the input of a number n and then find and display
its factorial (n!). For example, 5! = 5x4x3x2x1 i.e., 120.
num=int(input("Enter a number to find factorial:"))
fac=1
for x in range(1,num+1):
fac*=x
print("The factorial of ",num," is : ",fac)
[Link] a program to input a string and display the string in the reverse order.
word=input("Enter the string you want to print in reverse order : ")
l=len(word)
for x in range(l-1,-1,-1):
print(word[x])
[Link] a program to input a number and display whether it is a prime number or
not.
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num / 2) + 1):
if (num % i) == 0:
print(num, 'is not prime')
else:
break
print(num, "is a prime number")
else:
print(num, "is not a prime number")
[Link] a program to input two numbers and swap both the numbers without
using the third
x=int(input("Enter the value of x: "))
y=int(input("Enter the value of y: "))
x,y=y,x
print("Numbers after swapping: ",x,y)