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

PraKash Python

The document contains Python code snippets for various mathematical functions including calculating factorial, Fibonacci series, LCM, HCF, and checking for leap years. Each function includes user input handling and validation for appropriate values. The code demonstrates the use of recursion and built-in Python libraries for mathematical operations.

Uploaded by

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

PraKash Python

The document contains Python code snippets for various mathematical functions including calculating factorial, Fibonacci series, LCM, HCF, and checking for leap years. Each function includes user input handling and validation for appropriate values. The code demonstrates the use of recursion and built-in Python libraries for mathematical operations.

Uploaded by

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

Name:Prakash kumar P

Reno:1P23MC0042

Factorial function using recursion


def factorial(n):
if n == 1 or n == 0:
return 1
else:
return n * factorial(n - 1)

# User input
num = int(input("Enter a number: "))

# Checking for negative input


if num < 0:
print("Factorial is not defined for negative
numbers!")
else:
print("Factorial of", num, "is", factorial(num))
fibnocci series

def fibonacci(n):
if n <= 0:
return "Invalid input! Enter a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)

# Get user input


num = int(input("Enter the position of Fibonacci
number: "))

# Check for valid input


if num <= 0:
print("Please enter a positive integer.")
else:
print(f"The {num}th Fibonacci number is:",
fibonacci(num))

find lcm
import math

def lcm(a, b):


return abs(a * b) // math.gcd(a, b) # Using GCD
formula

# Get user input


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print(f"The LCM of {num1} and {num2} is: {lcm(num1,


num2)}")

hcf

import math
def hcf(a, b):
return math.gcd(a, b) # Using Python's built-in
function

# Get user input


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print(f"The HCF of {num1} and {num2} is: {hcf(num1,


num2)}")

leap year

import calendar
year = int(input("Enter a year: "))
if calendar.isleap(year):
print(f"{year} is a Leap Year")
else:
print(f"{year} is NOT a Leap Year")

You might also like