0% found this document useful (0 votes)
2 views3 pages

6 Program

Uploaded by

aumjha24
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)
2 views3 pages

6 Program

Uploaded by

aumjha24
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
You are on page 1/ 3

a) Python Program to Check if a Date is Valid and Print the Incremented Date if it is

b) Python program to find factorial of a number

def is_leap_year(year): # Check if a year is a leap year


return (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0))
def is_valid_date(year, month, day): # Check if the month is valid
if month < 1 or month > 12:
return False
# Define days in each month
days_in_month = [31, 29 if is_leap_year(year) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31]
# Check if the day is valid for the given month
return 1 <= day <= days_in_month[month - 1]
def increment_date(year, month, day): # Define days in each month
days_in_month = [31, 29 if is_leap_year(year) else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31]
day += 1 # Increment the day
# Check if we need to increment the month
if day > days_in_month[month - 1]:
day = 1
month += 1
# Check if we need to increment the year
if month > 12:
month = 1
year += 1
return year, month, day
# Input from the user
year = int(input("Enter year: "))
month = int(input("Enter month: "))
day = int(input("Enter day: "))
# Validate date and increment if valid
if is_valid_date(year, month, day):
next_year, next_month, next_day = increment_date(year, month, day)
print(f"Valid date. The next date is: {next_year}-{next_month:02d}-{next_day:02d}")
else:
print ("Invalid date.")

Python program to find factorial of a number

1. Using a Loop

def factorial_loop(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0 or n == 1:
return 1
else:
factorial = 1
for i in range(2, n + 1):
factorial *= i
return factorial
# Input from the user
number = int(input("Enter a number: "))
print(f"The factorial of {number} is: {factorial_loop(number)}")
2. Using Recursion

def factorial_recursive(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0 or n == 1:
return 1
else:
return n * factorial_recursive(n - 1)

# Input from the user


number = int(input("Enter a number: "))
print(f"The factorial of {number} is: {factorial_recursive(number)}")

You might also like