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

A6_Assignment

Uploaded by

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

A6_Assignment

Uploaded by

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

Part – A (Mandatory)

1. Define a user-defined function <sum_of_digits> for finding the sum, return


the sum of
the digits. Get input from the user and pass the arguments while calling the
function
<sum of digits>

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)

2. Define user-define functions <separation> and <prime finder>,which get


the input from
the user and separate the digits, then print the digits that are prime.( Usage of
<global>
keyword is mandatory).

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()

3. Generate the range of “N” values and find


a. the sum of odd values <find_sum_odd>
b. the sum of even values <find_sum_even>
#function defined for odd number addition
def sum_of_odd(x):
s=0
for i in range(1, x+1):
if i%2 != 0:
s = s+i
else:
continue
return(s)
#function defined for even number addition
def sum_of_even(x):
s=0
for i in range(1, x+1):
if i%2 == 0:
s=s+i
else:
continue
return(s)
#taking the input
n = int(input("What is the n value?"))

#calling the functions


a = sum_of_odd(n)
b = sum_of_even(n)

#displaying the output


print("The sum of the odd numbers is", a)
print("The sum of the even numbers is", b)

4. Pass a number to a function as parameter which finds the factors of the


given number
and displays it. If a number is not given, then display with default value.

def factors(x):
for i in range(1, x+1):
if x%i == 0:
print(i)
else:
continue

a = input("What is your number?")

if a.isnumeric():
a = int(a)
factors(a)
else:
factors(6)

5. Define user-defined function <sine_series> which computes the sum of the


series.

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))

7. Write a Python program to solve the Fibonacci sequence using recursion.

def fibonacci(x):
if x <= 1:
return x
else:
return (fibonacci(x-1)+fibonacci(x-2))

n = int(input("What is the n value?"))


for i in range (n):
print(fibonacci(i))

8. Write a Python program to get the sum of a non-negative integer.


Test Data:
sumDigits(345) -> 12,
sumDigits(45) -> 9
def sum_(n):
if n == 0:
return 0
else:
return(n%10+sum_(n//10))
x = int(input("What is your number?"))
a = sum_(x)
print("The sum of the digits is", a)

9. Write a Python program to calculate the sum of the positive integers of


n+(n-2)+(n-4)... (until n-x =< 0).
Test Data:
sum_series(6) -> 12,
sum_series(10) -> 30

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.

def gcd(a, b):


if a == b:
return a
elif a < b:
return gcd(b, a)
else:
return gcd(b, a-b)

m = int(input("What is a?"))
n = int(input("What is b?"))
print(gcd(m, n))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy