You are on page 1of 4

CS

QUESTIONS
Ques 46 & 47

QUES 46
A. * * * * *
* *
* *
* *
* * * * *
SOLUTION
def star_pattern(n) :
for i in range(1, n + 1) :
for j in range(1, n + 1) :
if i in [1, n] or j in [1, n] : # OR i==1 or
i==n or j==1 or j==n
print("*", end=" ")
else :
print(" ", end=" ")
print()
n = int(input("Enter no. of rows & columns : "))
star_pattern(n)

B. $ * * * *
* $ *
* $ *
* $ *
* * * * $
SOLUTION
def star_pattern(n) :
for i in range(1, n + 1) :
for j in range(1, n + 1) :
if i == j :
print("$", end=" ")
elif i in [1, n] or j in [1, n] : # OR i==1
or i==n or j==1 or j==n
print("*", end=" ")
else :
print(" ", end=" ")
print()
n = int(input("Enter no. of rows & columns : "))
star_pattern(n)

C. $ * * * $
* $ $ *
* $ *
* $ $ *
$ * * * $
SOLUTION
def star_pattern(n) :
for i in range(1, n + 1) :
for j in range(1, n + 1) :
if i == j or j == (6 - i) :
print("$", end=" ")
elif i in [1, n] or j in [1, n] : # OR i==1
or i==n or j==1 or j==n
print("*", end=" ")
else :
print(" ", end=" ")
print()
n = int(input("Enter no. of rows & columns : "))
star_pattern(n)
QUES 47
A. -x + x2 - x3 + x4 + …
SOLUTION
def sum_series(x, n) :
sum = 0
f = -1
for i in range(1, n + 1) :
term = f*(x**i)
f *= -1
sum += term
return sum
x = int(input("Enter a value for x : "))
n = int(input("Enter a value for n : "))
print("Sum of the series(-x + x^2 - x^3 + x^4 + ...) :",
sum_series(x, n))
SAMPLE OUTPUT
Enter a value for x : 2
Enter a value for n : 4
Sum of the series(-x + x^2 - x^3 + x^4 + ...) : 10

B. 1 - x + x^2/2! - x3/3! + …
SOLUTION
def sum_series(x, n) :
sum = 1
f = -1
for i in range(1, n + 1) :
term = f*(x**i)
f *= -1
fact = 1
for j in range(1, i + 1) :
fact *= j
sum += term / fact
return sum
x = int(input("Enter a value for x : "))
n = int(input("Enter a value for n : "))
print("Sum of the series(1 - x + x/2! - x^3/3! + ...) :",
sum_series(x, n))
SAMPLE OUTPUT
Enter a value for x : 2
Enter a value for n : 3
Sum of the series(1 - x + x^2/2! - x^3/3! + ...) :
-0.33333333333333326

— By ռɨȶɨռ ␢ɦǟȶȶ

You might also like