0% found this document useful (0 votes)
23 views4 pages

10 Codes

Uploaded by

aaryankhan0711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

10 Codes

Uploaded by

aaryankhan0711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#prime

def test_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
if test_prime(num):
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")

#factorial using fucntion


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

n = int(input("Enter a number: "))


print(factorial(n))

#factorial using lambda


factorial = lambda n:1 if n == 0 else n * factorial(n - 1)
n = int(input("Enter a number: "))
print(factorial(n))

#add,sub
def add(a, b):
return a + b
def sub(a, b):
return a - b
def main():
n1 = float(input("Enter the first number: "))
n2 = float(input("Enter the second number: "))
sum_result = add(n1, n2)
diff_result = sub(n1, n2)
print(f"The sum of {n1} and {n2} is: {sum_result}")
print(f"The difference between {n1} and {n2} is: {diff_result}")
main()

#palindrome with loop


def test_palindrome(s):
s = s.lower()
start = 0
end = len(s) - 1
while start < end:
if s[start] != s[end]:
return False
start += 1
end -= 1
return True

def main():
user_input = input("Enter a string to check if it is a palindrome: ")
if test_palindrome(user_input):
print(f"'{user_input}' is a palindrome!")
else:
print(f"'{user_input}' is not a palindrome.")
main()

#palindrome without loop


def test_palindrome(s):
return s == s[::-1]
def main():
user_input = input("Enter a string to check if it is a palindrome: ")

if test_palindrome(user_input):
print(f"'{user_input}' is a palindrome!")
else:
print(f"'{user_input}' is not a palindrome.")
main()

#fibonacci using function


def fibonacci(n):
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
def main():
n = int(input("Enter the number of terms: "))
print("Fibonacci sequence:")
fibonacci(n)
main()

#armstrong
def armstrong_number(num):
digits = str(num)
num_digits = len(digits)
sum_of_powers = sum(int(digit) ** num_digits for digit in digits)
return sum_of_powers == num
number = int(input("Enter a number: "))

if armstrong_number(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")

#sine series
import math
def sine_series(x, n_terms):
sine_value = 0
for n in range(n_terms):
term = ((-1) ** n) * (x ** (2 * n + 1)) / math.factorial(2 * n + 1)
sine_value += term
return sine_value

x = float(input("Enter the angle in radians: "))


n_terms = int(input("Enter the number of terms to approximate: "))

result = sine_series(x, n_terms)


print(f"The sine of {x} is: {result}")

#cosine series
import math
def cosine_series(x, n_terms):
cosine_value = 0
for n in range(n_terms):
term = ((-1) ** n) * (x ** (2 * n)) / math.factorial(2 * n)
cosine_value += term
return cosine_value

x = float(input("Enter the angle in radians: "))


n_terms = int(input("Enter the number of terms to approximate: "))

result = cosine_series(x, n_terms)


print(f"The cosine of {x} is: {result}")

#calculator
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error! Division by zero."
return x / y

def calculator():
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")

if choice in ['1', '2', '3', '4']:


n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))

if choice == '1':
print(f"{n1} + {n2} = {add(n1, n2)}")

elif choice == '2':


print(f"{n1} - {n2} = {subtract(n1, n2)}")

elif choice == '3':


print(f"{n1} * {n2} = {multiply(n1, n2)}")

elif choice == '4':


print(f"{n1} / {n2} = {divide(n1, n2)}")
else:
print("Invalid Input! Please select a valid operation.")

calculator()

You might also like