11th Cbse Cs Lab Manual
11th Cbse Cs Lab Manual
if (num1>num2):
else:
print ("The Larger number is", num2)
if (num1<num2):
else:
print ("The Smaller number is", num2)
else:
print ("The Largest number is", num3)
else:
print ("The Smallest number is", num3)
* 12345 A
** 1234 AB
*** 123 ABC
**** 12 ABCD
***** 1 ABCDE
Pattern-1:
def pypart(n):
mylist = []
for i in range (1, n+1):
mylist.append("*" *i)
print("\n".join(mylist))
n=5
pypart(n)
Pattern-2:
n=int(input("Enter no of rows:"))
for i in range (n, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print("\n")
Pattern-3:
n=int(input("Enter number of rows:"))
for i in range(n):
a=65
for j in range(i+1):
p rint(chr(j+65),end="")
a+=1
print("\n")
5.Write a program to input the value of x and n and print the sum of the following series:
1.
2.
3.
4.
Code-1:
x=float(input(“Enter the value of x:”))
n=int(input(“Enter the value of n:”))
sum=0
for i in range(n+1):
sum=sum+x**i
print(“The sum of the series is:”, sum)
Code-2:
x=float(input(“Enter the value of x:”))
n=int(input(“Enter the value of n:”))
sum=0
for i in range(n+1):
if(i%2==0):
sum=sum+(x**i)
else:
sum=sum-(x**i)
print(“The sum of the series is:”, sum)
Code-3:
x=int(input("Enter the value of x:"))
n=int(input("Enter the value of n:"))
sum=x
for i in range (2,n+1):
if i%2==0:
sum=sum+(x**i)/i
else:
sum=sum-(x**i)/i
print(“sum of series is:", sum)
Code-4:
import math
x=int(input("Enter the value of x:"))
n=int(input("Enter the value of n:"))
sum=x
for i in range(2,n+1):
if(i%2==0):
sum=sum+(x**i)/(math.factorial(i))
else:
sum=sum-(x**i)/(math.factorial(i))
print("sum of series is:", sum)
6.Determine whether a number is a perfect number, an Armstrong number or a palindrome.
num = int(input("Enter a number: "))
sum = 0
for i in range(1, num):
if num % i == 0:
sum += i
if sum == num:
print(num, "is a perfect number.")
else:
print(num, "is not a perfect number.")
temp=num
total=0
while temp>0:
digit=temp%10
total=total+(digit**3)
temp=temp//10
if num == total:
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")
temp=num
rev=0
while num>0:
d=num%10
rev=rev*10+d
num=num//10
if temp==rev:
print(temp,"is a palindrome number")
else:
print(temp,"is not a palindrome number")
7.Input a number and check if the number is a prime or composite number.
num = int(input("Enter any number : "))
if num > 1:
for i in range(2, num):
if (num % i == 0):
print(num, "is NOT a PRIME number, but it is a COMPOSITE number")
break
else:
print(num, "is a PRIME number but not composite number")
elif (num == 0 or num == 1):
print(num, "is a neither Prime NOR Composite number")
else:
print(“Please enter positive number only”)
8. Display the terms of a Fibonacci series.
nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
9. Compute the greatest common divisor and least common multiple of two integers.
n1=int(input("Enter the first number:"))
n2=int(input("Enter the second number:"))
x=n1
y=n2
while(n2!=0):
t=n2
n2=n1%n2
n1=t
gcd=n1
print("GCD of {0} and {1} = {2}".format(x,y,gcd))
lcm=(x*y)/gcd
print("LCM of {0} and {1} = {2}".format(x,y,lcm))
10. Count and display the number of vowels, consonants, uppercase, lowercase characters in
string.
s=input("Enter any string:")
vowel=consonant=uppercase=lowercase=0
for i in s:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or
i=='U'):
vowel=vowel+1
else:
consonant=consonant+1
if i.isupper():
uppercase=uppercase+1
if i.islower():
lowercase=lowercase+1
print("no of vowels:",vowel)
print("no of consonants:",consonant)
print("no of uppercase:",uppercase)
print("no of lowercase:",lowercase)
11. Input a string and determine whether it is a palindrome or not; convert the case of
characters in a string.
s=input("Enter any string")
j=-1
flag=0
for i in s:
if(i!=s[j]):
flag=1
break
j=j-1
if(flag==0):
print("This string is a palindrome")
else:
print("This string is not a palindrome")
sc=s.swapcase()
print("String after converting the case of each character:",sc)
12. Find the largest/smallest number in a list/tuple
list1 = []
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
element= int(input("Enter elements: "))
list1.append(element)
print("Smallest element in List1 is:", min(list1))
print("Largest element in List1 is:", max(list1))
13. Input a list of numbers and swap elements at the even location with the elements at the
odd location.
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
# printing original list
print("The original list : " + str(mylist))
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(mylist)):
if i % 2==0:
even_i.append(mylist[i])
else :
odd_i.append(mylist[i])
result = odd_i + even_i
# print result
print("Separated odd and even index list: " + str(result))
14. Input a list/tuple of elements, search for a given element in the list/tuple.
mylist = []
print("Enter 5 elements for the list: ")
for i in range(5):
value = int(input())
mylist.append(value)
print("Enter an element to be search: ")
element = int(input())
for i in range(5):
if element == mylist[i]:
print("\nElement found at Index:", i)
print("Element found at Position:", i+1)
break
else:
print("Invalid input")
15. Create a dictionary with the roll number, name and marks of n students in a class and
display the names of students who have marks above 75.
no_of_std = int(input("Enter number of students: "))
result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
# Display names of students who have got marks more than 75
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks is/are",(result[student][0]))