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

Basic Python PROGRAMS

The document contains five Python programs demonstrating basic programming concepts. Program 1 checks if a year is a leap year, Program 2 finds the greatest of three numbers using nested if statements, Program 3 checks if a number is prime, Program 4 calculates the sum of elements in a list, and Program 5 computes the factorial of a number using recursion.

Uploaded by

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

Basic Python PROGRAMS

The document contains five Python programs demonstrating basic programming concepts. Program 1 checks if a year is a leap year, Program 2 finds the greatest of three numbers using nested if statements, Program 3 checks if a number is prime, Program 4 calculates the sum of elements in a list, and Program 5 computes the factorial of a number using recursion.

Uploaded by

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

Program 1: WAP to input a year and check whether the year is leap year or not.

y=int(input("Enter the year: "))


if y%400==0:
print("The year is a Century Leap Year")
elif y%100!=0 and y%4==0:
print("Year is a Leap year")
else:
print("Year is not a leap year")

output :
Program 2 : WAP to input 3 numbers and print the greatest number using nested if.

a=float(input("Enter the first number: "))


b=float(input("Enter the second number: "))
c=float(input("Enter the third number: "))
if a>=b:
if a>=c:
print("First number :",a,"is greatest")
if b>a:
if b>c:
print("Second number :",b,"is greatest")
if c>a:
if c>b:
print("Third number :",c,"is greatest")

output :
Program 3 : WAP to input a number and check whether it is prime number or not.

n=int(input("Enter the number "))


c=1
for i in range(2,n):
if n%i==0:
c=0
if c==1:
print("Number is prime ")
else:
print("Number is not prime ")

output :
Program 4 : WAP to find sum of elements in list

total = 0
# creating a list
list1 = [11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variable total
for ele in range(0, len(list1)):
total = total + list1[ele]

# printing total value


print("Sum of all elements in given list: ", total)

Output :
Program 5. WAP to find Factorial of a number using recursion

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = 7
# check if the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))

output :
:

You might also like