Chapter 6 Solution PDF
Chapter 6 Solution PDF
Chapter 6 Solution PDF
A.
1. C
2. A, B
3. C
4. D
5. B
6. D
7. B
8. A
9. D
10. D
11. B
12. C
13. B
B.
1. True
2. True
3. False
4. True
5. False
6. False
7. True
8. True
9. True
10. True
C.
1. Advantages of using function are as follows
a. Complex problems can be decomposed into smaller
modules/pieces.
b. Reduce duplication of code
c. Reusability of code
def calc(r):
return 3.14*r*r
x = (int(input('Enter the radius:')))
print(' The are of circle is',calc(x)) #X passed as argument to calc()
Output
Enter the radius:5
78.5
In the above program, we have defined calc() as function which perform
its task when called. Thus before calling we have read the radius of
circle as input from the user and the same has been passed as argument
to function calc(). Once the function is called it takes value of x as
radius, calculate area and return back area of circle to the print
statement.
7.
def area(p,q):
print(p*q)
l = 10
b = 20
area(l,b) #Arguments l and b passed to a function
8.
def sq(x):
return x*x
print(sq(x=20))
10.
Local Variables:
Variables declared within functions are referred to be as local variable
and all those variables are local to the function. The following program
illustrate the concept of local variable as follows
def Scope_Demo(x):
x = x +10
print(' The value of x(within local scope) is :',x)
x = 50
Scope_Demo(x)
print(' Still value of x is:',x)
Output
The value of x(within local scope) is : 60
Still value of x is: 50
From the above program, we can conclude the variable defined inside
function is local to the function and the value of the local variable
remains unaffected outside the function.
Global variables:
x = 30 #Global Variable
def Demo():
y = 10 # Local Variable
print(' The value of local variable y is :',y)
print(' The value of global variable x is :',x)
Demo()
Output
The value of local variable y is : 10
The value of global variable x is : 30
import math
def eval_Quadratic_Equa(a, b ,c, x):
d = (b**2) - 4*a*c
if d < 0:
print('The equation has no real solution')
elif d == 0:
x = (- b + math.sqrt(d))/ (2*a)
print(' This equation has one solution:',x)
else:
x1 = (-b - math.sqrt(d))/(2*a)
x2 = (-b + math.sqrt(d))/(2*a)
print(' Equation has two solutions: ',format(x1,'.2f'),' and
',format(x2,'.2f'))
Output
Enter the value of a:1
Enter the value of b:0
Enter the value of c:-48
Equation has two solutions: -6.93 and 6.93
def calc_exp(base,exp):
print(base**exp)
base = float(input('Enter the value of base:'))
exp = int(input('Enter the value of exponent:'))
calc_exp(base,exp)
Output
Enter the value of base:5
Enter the value of exponent:3
125.0
Note: The greatest common divisor (GCD) of two positive integers is the
largest integer that divides each of them without a remainder.
Example:
gcd(12 , 2) = 2
gcd(6 , 12) = 6
gcd(9 , 12) = 3
Output
Enter the value of a:9
Enter the value of b:12
3
def reverse_number(num):
rev = 0
while(num > 0):
rev = (10*rev)+num%10
num //= 10
return rev
Output
Enter the number:234
432
Output
Enter the number:5433
15
With Recursion
def Calc_Sum_Digits(num):
rem = 0
sum1 = 0
if num == 0:
return sum1
else:
return sum1 + num%10 + Calc_Sum_Digits(num//10)
Output
Enter the number:123456789
45
def factors(num):
print('Factors of ',num,' are as follows: ')
for x in range(1,num+1):
if num % x == 0:
print(x, end = ' ')
else:
pass
num = int(input('Please Enter the number:'))
factors(num)
Output
Please Enter the number:120
Factors of 120 are as follows:
1 2 3 4 5 6 8 10 12 15 20 24 30 40 60
def Dec_to_Bin(n):
if n > 1:
Dec_to_Bin(n//2)
print(n % 2,end = '')