You are on page 1of 10

DELHI PUBLIC SCHOOL – BANGALORE (SOUTH)

LAB ACTIVITY – 1
TOPIC: CONDITIONAL AND LOOPING CONSTRUCTS
SUBJECT: COMPUTERS CLASS: 11

1. Write a menu based program to print the following patterns, by accepting number of
lines:

2. Develop codes for any 5 of the following.


a. b. c. d.

e. f.
3. WAP to print the Fibonacci series till n terms (Accept n from user).
n=int(input("enter the number of terms: "))
f=0
print(f)
s=1
print(s)
for i in range(2,n):
nxt=f+s
print (nxt)
f=s
s=nxt

4. WAP to find the sum of the following series(accept values of x and n from user)
1 - x/1! + x2/2! - ……….xn/n!
x = int(input("Enter the value of x: "))
n=int(input("Enter the value of n : "))
sum = 0
m=1

for i in range(1, n+1) :


fact = 1
for j in range(1, i+1) :
fact *= j
term = x ** i / fact
sum += term * m
m = m * -1

print("Sum =", 1+sum)


5. WAP to accept a number and check whether it is palindrome or not (without
converting the number into sequence).
n=int(input("Enter a number : "))
temp=n
rev=0
while(n>0):
dig = n% 10
rev = rev * 10 + dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number is not a palindrome!")

6. WAP to generate prime number within a range input by the user.


lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
7. WAP to Compute roots of a quadratic equation
import math as m
a=int(input("enter the value of a in ax^2+bx+c "))
b=int(input("enter the value of b in bx^2+bx+c "))
c=int(input("enter the value of c in ax^2+bx+c "))
if a==0:
print("a can not be zero enter a different value ")
else:
d=b*b-4*a*c
if d>0:
print("roots are real and distinct")
r=(-b+m.sqrt(d))/(2*a)
f=(-b-m.sqrt(d))/(2*a)
print("roots are",r,f)
elif d==0:
print("roots are real and equal")
r2=-b/2*a
print("roots are",r2)
elif d<0:
print("roots are imaginary"
pattern Questions
n=int(input("enter a number "))
for i in range(n):
for j in range(i+1):
if((i+j)%2==0):
print("1",end=' ')
else:
print("0",end=' ')
print()
o/p
1
01
101
0101
n=int(input("enter number of rows: "))
for i in range (1,n+1):
for j in range (1,2*n):
if i==n or i+j==n+1 or j-i==n-1:
print("*",end='')
else:
print(end=" ")
print()
n = int(input('Enter the rows ? '))
row=n//2
for i in range(1,row+1):
for j in range(1,i+1):
print(j, end='')
for j in range(i-1,0,-1):
print(j, end='')
print()
for k in range(row-1,0,-1):
for r in range(1,k+1):
print(r, end='')
for r in range(k-1,0,-1):
print(r, end='')
print()
row=int(input("enter a number of rows"))
l=1
for i in range(1,row*2,2):
for j in range (1, (row - l) + 1):
print(end = " ")
for j in range(i,i*2):
print(i,end='')
l=l+1
print()
d=1
for k in range(row*2-3,0,-2):
for j in range (1,d+1):
print(end = " ")
for r in range(k,k*2):
print(k,end='')
d=d+1
print()
row = int(input('Enter the rows ? '))
l=1
n=row
for i in range(row,0,-1):
for s in range (1, (row - l) + 1):
print(end = " ")
for j in range(i,row+1,1):
print(j, end='')
for j in range(row-1,i-1,-1):
print(j, end='')
l=l+1
print()
user=int(input(' choose an option choice 1:pattern A or choice 2:pattern B '))
if user==1:
row=int(input("enter a number : "))#code for pattern 1
u=65+row
for k in range (65,u):
print(chr(k),end='')
for t in range (u-2,64,-1):
print(chr(t),end='')
print()
l=1
for i in range (u-1,65,-1):
for j in range(65,i):
print(chr(j),end='')
for s in range (1,l*2):
print(end = " ")
for j in range(i-1,64,-1):
print(chr(j),end="")
l=l+1
print()
if user==2:
x = int(input("enter the number of lines :"))#pattern number two code
u=(x+65)-1
for i in range (u,64,-1):
for j in range(u,i-1,-1):
print(chr(j),end='')
print()
for k in range (u-1,64,-1):
for l in range(k,64,-1):
print(chr(l),end='')
print()

if input is 1
and the rows is 7

if input is 2
and the rows is 26

You might also like