We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
In (a):
In [2]:
out [2]:
In [3]:
In [4]:
created by Amar Sharma
Q16) Fibonacci
Write a program to find a fibonacc! of a number.
def Fibo(x)
Af x eed
return [1]
a= [1,2]
for i in range(3,x+1):
‘[Link](a[-1]#a[-2])
return a
Fibo(8)
[1, 2, 3, 5, 8, 13, 21, 34]
Q17) Pattern
Write a program to print the following pattern.
def maket(x)
for i in range(1,x+1):
print ("+ "4 )
naker(s)
Q18) Odd or Even
Write a program to print the given number is odd or even.In (5):
In [6]:
out[6]:
In [7]:
out [7]:
In [8]:
In [9]:
out [9]:
In [10]:
out [10]:
def even_odd(x)
if x2
return “Even”
else:
return "odd"
even_odd(4)
“even"
even_odd(7)
“oda
Q19) Number Palindrome
Write a program to check ifthe given number is palindrome or not
def num_palindrome(x)
a3 = str(x)
b3 = aB[:z-2]
return a:
‘nun_palindrome(123321)
True
‘nun_palindrone (121212125)
False
Armstrong number (also known as a narcissistic number or plenary number) is a number that is equal
to the sum of its own digits each raised to the power of the number of digits.
For example, let's take the number 153:
+ Number of digits: 3
+ Each digit raised to the power of the number of digits:
("3
16S
+ G3
+ Sum of these values: (1 + 125 + 27 = 153)
So, 153 is an Armstrong number because itis equal to the sum of its own digits each raised to the power
of the number of digits.
Q20) Armstrong
Write a program to check if the given number is Armstrong or not.In [14]:
In [22]:
out [12]:
In [13]:
out [13]:
In [14]:
In [15]:
out [15]:
In [16]:
In [17]:
out [17]:
def check_arnstrong(x):
a19 = str(x)
bis = len(a1s)
9 =@
ais = @
for i in aia:
19 += int(i) ** bis
return x == 19
check_armstrong(153)
True
check_armstrong(125)
False
21) Maximum of two numbers
Write a program to find a maximum of two numbers.
def check_max(x,y)
if wy:
return x
else:
return y
check_max(45, 23)
45
Q22) Minimum of two numbers.
Write a program to find a minimum of two numbers.
def check min(x,y)
if wy:
return y
else:
return x
check_min(54,23)
23
Q23) Maximum of three numbers
Write a program to find a maximum of three numbers.In [18]:
In [19]:
out [19]:
In [20]:
In [2a]:
out (21):
In [22]:
In [23]:
out [23]:
In [24]:
In [25]:
out [25]:
def check max(x,y,z)+
if yomax:
max = y
if zomax:
return max
check_max(6,2,5)
6
# Another method
def check max1(x):
222 = max(x)
return a22
check_max(([1,55,8,46])
55
4 Another Method
def check_max2(x):
max = x{@]
for i in x[t:]:
if > nai
return max
check _max2([55,2222,1,7,6,889,5])
2222
Q24) Minimum of three numbers
Write @ program to find a minimum of three numbers.
def check min(x,y,2)!
if yemin:
Af zemi
y
return min
check_min (5,8,6)
5In [26]:
In [27]:
out [27]:
In [28]:
In [29]:
out [29]:
In [30]:
In [34]:
out (31):
Q25) Factorial
Write a program to find a factorial of a number.
def fac(x):
a=.
for i in range(1,x+1):
return a
fac(s)
120
import math
math. factorial (5)
120
Q26) GCD ( Greatest Common Divisor ) also callled HCF
Write a program to find GCD of two numbers.
def heF(x,y)
a= (]
for i in range (1,min(x,y)+1):
if x M4 and yi == 0:
‘[Link](i)
return max(a)
he #(4s,54)
9
Q27) Pattern
Write a program to print the following pattern.In [32]:
In [33]:
In [34]:
In [35]:
def make2(rows) :
for i in range(1, rows + 1):
print(" " * (rows - i) +
"*@)
# Example usage:
make2(5)
Q28) Pattern
Write a program to print the following pattern.
RNNN
def make6(x):
for i in range (1,x+1):
print(" “.join(str(j) for j in range(1,i+1)))
make6(S)
1
te
ibs
1234
1234s
Q29) Pattern
Write a program to print the following pattern.
1
23
456
7 8 9 10
1112 13 14:15
def make6(x):
as.
for i in (Gyxe):
print(" ".join(str(j) for 5 in range(a,ati)))In [36]:
In [37]:
make6(5)
112 13:14 15,
Q30) Pattern
Write a program to print the following pattern.
monw>
mone
mon
mo
def make7(rows):
a= ord("A') # ASCIT value of ‘A
for i in range(rows):
char = chr(a + i) # Convert ASCII value to character
print((chars” ") * (4 + 2)
make7(S)
Q31) Pattern
Write a program to print the following pattern.
AOOS>
rxIomon
zon
2S
fo}In [38]:
In [39]:
In [40]:
out [40]:
In [41]:
out [41]:
In [42]:
out [42]:
def make8(rows):
a= ord("A') # ASCII value of ‘A
for i in range(rows):
for j in range(i + 1):
char = chr(a + j +4) # Convert ASCII value to character
print(char +" ", end="")
print()
make8(5)
a
Bc
coe
DEFG
EFGHI
Q32) Positive or Negative
Write a program to find the given number is positive or negative.
def check(x):
Af xo:
return
2
return
else:
return "Positive"
"Negative"
elif
Zero"
check(0)
“Zero’
check(-8)
‘Negative’
check(5)
‘Positive’