A6_Assignment
A6_Assignment
def sum_of_digits(x):
s=0
b=0
while x > 0:
b = x % 10
s=s+b
x = x//10
return s
a = int(input("What is your number?"))
t = sum_of_digits(a)
print("The sum of the digits of the number is", t)
def separation():
number = int(input("Enter number:"))
n = number
global n
while n > 0:
digit = n % 10
if check_prime(digit):
print(digit)
n = n // 10
def check_prime():
if n > 1:
for i in range(2, n):
if (n%i) == 0:
return False
else:
return True
else:
return False
separation()
def factors(x):
for i in range(1, x+1):
if x%i == 0:
print(i)
else:
continue
if a.isnumeric():
a = int(a)
factors(a)
else:
factors(6)
import math
def sin(x, b):
sine = 0
for i in range(n):
sign = (-1)**i
pi = 22/7
y = x*(pi/180)
sine += ((y**(2.0*i+1))/math.factorial(2*i+1))*sign
return sine
a = int(input("What is x?"))
n = int(input("What is n"))
print(round(sin(a,n), 2))
Recursive:
6. Write a Python program to calculate factorial of a given number using
recursive func-
tion. The base case should handle the negative integers by printing an error
message
and returns none to indicate that something went wrong
def funct(n):
if n==1 or n==0:
return 1
elif n<0:
return "error"
else:
return(n * funct(n - 1))
qa = int(input("Enter the number: "))
print(funct(qa))
def fibonacci(x):
if x <= 1:
return x
else:
return (fibonacci(x-1)+fibonacci(x-2))
def sum_of_series(n):
if n == 0:
return 0
else:
return(n + sum_of_series(n-2))
a = int(input("What is the n value?"))
b = sum_of_series(a)
print("The sum of the series is", b)
10. The greatest common divisor (GCD) of a and b is the largest number that
divides both
of them with no remainder. One way to find the GCD of two numbers is based
on the
observation that if r is the remainder when a is divided by b, then gcd(a, b) =
gcd(b, r).
As a base case, we can use gcd(a, 0) = a. Write a recursive function called gcd
that takes
parameters a and b and returns their greatest common divisor.
m = int(input("What is a?"))
n = int(input("What is b?"))
print(gcd(m, n))