0% found this document useful (0 votes)
17 views

CS winter break solution (1)[1]

Uploaded by

mrzerogaming8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

CS winter break solution (1)[1]

Uploaded by

mrzerogaming8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Bedi International

School

Practical File
Submitted To: Mrs. Amoli (PGT Comp. Sci.)
Submitted By: Shikhar Kumar XI-A
Class Teacher: Mr. Pankaj Kunzru
Table Of Contents
1. Acknowledgement
2. List of Problems
3. Problems
i) Problem 1
ii) Problem 2
iii) Problem 3
iv) Problem 4
v) Problem 5
vi) Problem 6
vii) Problem 7
viii) Problem 8
ix) Problem 9
x) Problem 10
xi) Problem 11
xii) Problem 12
xiii) Problem 13
xiv) Problem 14
xv) Problem 15
xvi) Problem 16
xvii) Problem 17
xviii) Problem 18
xix) Problem 19
xx) Problem 20
xxi) Problem 21
xxii) Problem 22

Acknowledgement
I, Shikhar Kumar, would like to express my sincere gratitude to Mrs.
Amoli Chakroborty for the guidance and support provided throughout the
completion of my Computer Science practical file. Your valuable
feedback and encouragement have been instrumental in shaping my
understanding and skills in the field.
I would also like to extend my appreciation to Mrs. JK Sawhney, our
esteemed School Principal, for fostering an environment that encourages
academic excellence and creativity
This practical file has been a significant learning experience for me, and I
am thankful for the opportunity to explore and apply the concepts learned
in the classroom. I believe that this practical file has not only enriched my
understanding of Computer Science but has also honed my problem-
solving and critical thinking abilities.
Once again, thank you, Mrs. Amoli Chakroborty, for your unwavering
support, and thank you, Mrs. JK Sawhney, for providing a platform for
students to excel and showcase their talents.
Sincerely,
Shikhar Kumar

List of Problems
1. Input a welcome message and display it.
2. Input two numbers and display the larger / smaller number.
3. Input three numbers and display the largest / smallest number.
4. Write a program to accept three numbers and print their sum.
5. Write a program to accept the length and breadth of a rectangle and
calculate its area.
6. Generate the following patterns using nested loop:
(i) *
**
***
****
*****
(ii) 12345
1234
123
12
1
(iii) A
AB
ABC
ABCD
ABCDE
7. Write a program to input the value of x and n and print the sum of the
following series:
(i) 1+x+x2+x3+x4+……..+xn
(ii) 1-x+x2-x3+x4-……..+xn
(iii) x-x2/2+x3/3-x4/4+……..+xn/n
(iv) x+x2/2!+x3/3!+x4/4!+……..+xn/n!
8. Determine whether a given number is a perfect number or not.
9. Determine whether a given number is Armstrong number or not.
10. Determine whether a given number is palindrome or not.
11. Input a number and check whether it is prime or composite.
12. Display the first n terms of the Fibonacci series where n is entered
by the user.
13. Compute the greatest common divisor and least common multiple
of two integers.
14. Count and display the number of vowels,consonants,uppercase and
lowercase characters in a string.
15. Input a string and state whether it is palindrome or not.
16. Input a string and convert the case of characters in the string.
17. Write a program to enter a number and print its reverse.
18. Find the largest/smallest number in a list/tuple.
19. Input a list of numbers and swap elements at the even location with
the elements at the odd location.
20. Input a list/tuple of elements, search for a given element in the
list/tuple.
21. Input a list of numbers and find the smallest and largest number
from the list.
22. Create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have scored
marks above 75.
Solutions
Q-1) Input a welcome message and display it.

Ans :

message=input("Enter welcome message : ")

print("Hello, ",message)

Q-2) Input two numbers and display the larger / smaller number.

Ans :

n1=int(input("Enter First Number: "))

n2=int(input("Enter Second Number: "))

if (n1>n2):

print("The Larger number is", n1)

else:

print ("The Larger number is", n2)


Q-3) Input three numbers and display the larger / smaller number.

Ans :

num1=int(input("Enter First Number: "))


num2=int(input("Enter Second Number: "))
num3=int(input("Enter Third Number: "))

if (num1> num2 and num1> num3):


print("The Largest number is", num1)

elif (num2 > num1 and num2> num3):


print ("The Largest number is", num2)
else:
print ("The Largest number is", num3)

Q-4) Write a program to accept three numbers and print their sum.
Ans :

num1=int(input("Enter Number 1 :
")) num2=int(input("Enter Number
2 : ")) num3=int(input("Enter
Number 3 : "))
sum=num1+num2+num3

print("Three Numbers are : ",num1,num2,num3)


print("The sum is : ",sum)

Q-5) Write a program to accept the length and breadth of a rectangle and calculate its area.
Ans :
length=float(input("Enter the length of rectangle : "))
breadth=float(input("Enter the breadth of rectangle : "))
area=length*breadth

print("Length : ",length," Breadth : ",breadth," Area : ",area)


Q-6 Generate the following patterns using nested

loop: (i)

**

***

****

*****

Ans :

for i in range(1,6):

for j in range(i):

print('*',end=' ')

print( )

(ii)

12345
1234
123
12
1

Ans :
for i in range(6,0,-1):
for j in
range(1,i):
print(j,end= ‘ ‘)
print ( )
(iii)
A
AB
AB
C
ABCD
ABCDE

Ans :
n=5
for i in range(n):
p=65
for j in range(i+1):
print(chr(p),end= ' ')
p+=1
print()

Q-7 Write a program to input the value of x and n and print the sum of the following series:
(i) 1+x+x2+x3+x4+…......+xn
Ans :

x = float(input("Enter x: "))

n = int(input("Enter n: "))

result1 = 0

power1 = 1

for i in range(n + 1):

result1 += power1

power1 *= x

print("Series (i) sum:", result1)


(ii) 1-x+x2-x3+x4...........+xn
Ans :
x = float(input("Enter x: "))

n = int(input("Enter n: "))

result2 = 0

power2 = 1

for i in range(n + 1):

result2 += power2 * ((-1) ** i)

power2 *= x

print("Series (ii) sum:", result2)

(iii) x-x2/2+x3/3-x4/4+…......+xn/n
Ans :
x = float(input("Enter x: "))
n = int(input("Enter n: "))
result3 = 0
power3 = 1
for i in range(1, n + 1):
result3 += power3 / i * ((-1) ** (i - 1))
power3 *= x
print("Series (iii) sum:", result3)

(iv) x+x2/2!+x3/3!+x4/4!+…......+xn/n!
Ans :
x = float(input("Enter x: "))
n = int(input("Enter n: "))
result4 = 0
power4 = 1
for i in range(1, n + 1):
fact = 1
for j in range(1, i + 1):
fact *= j
result4 += power4 /
fact power4 *= x
print("Series (iv) sum:", result4)

Q-8 Determine whether a given number is a perfect number or


not. Ans :
num = int(input("Enter a number: "))
divisors_sum = 0
for i in range(1, num):
if num % i == 0:
divisors_sum += i
if divisors_sum == num:
print(num, "is a perfect
number.") else:
print(num, "is not a perfect number.")

Q-9 Determine whether a given number is an Armstrong number or


not. Ans :
num = int(input("Enter a number: "))
num_str = str(num)
length = len(num_str)
armstrong_sum = 0
for digit in num_str:
armstrong_sum += int(digit) ** length
if armstrong_sum == num:
print(num, "is an Armstrong
number.") else:
print(num, "is not an Armstrong number.")

Q-10 Determine whether a given number is palindrome or


not. Ans :
num = input("Enter a number: ")
if num == num[::-1]:
print(num, "is a palindrome
number.") else:
print(num, "is not a palindrome number.")

Q-11 Input a number and check whether it is prime or


composite. Ans :
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(num, "is a composite
number.") break
else:
print(num, "is a prime number.")
else:
print(num, "is neither prime nor composite.")
Q-12 Display the first n terms of the Fibonacci series where n is entered by the
user. Ans :
n = int(input("Enter the number of terms: "))
a, b = 0, 1
count = 0
if n <= 0:
print("Please enter a positive
integer.") elif n == 1:
print("Fibonacci series up to", n, "terms:")
print(a)
else:
print("Fibonacci series up to", n, "terms:")
while count < n:
print(a, end=" ")
c=a+b
a=b
b=c
count += 1

Q-13 Compute the greatest common divisor and least common multiple of two
integers. Ans :
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
a, b = num1, num2
while b:
a, b = b, a % b
gcd_result = a
lcm_result = abs(num1 * num2) // gcd_result
print("GCD:", gcd_result)
print("LCM:", lcm_result)

Q-14 Count and display the number of vowels,consonants,uppercase and


lowercase characters in a string.
Ans :
string = input("Enter a string: ")
vowels, consonants, uppercase, lowercase = 0, 0, 0, 0
for char in string:
if char.isalpha():
if char.lower() in
'aeiou': vowels += 1
else:
consonants += 1
if char.isupper():
uppercase += 1
if char.islower():
lowercase += 1

print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase:", uppercase)
print("Lowercase:", lowercase)

Q-15 Input a string and state whether it is palindrome or


not. Ans :
string = input("Enter a string: ")
if string == string[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

Q-16 Input a string and convert the case of characters in the


string. Ans :
string = input("Enter a string: ")
print("Uppercase:", string.upper())
print("Lowercase:",
string.lower())
Q-17 Write a program to enter a number and print its
reverse. Ans :
num = input("Enter a number: ")
print("Reverse:", num[::-1])

Q-18 Find the largest/smallest number in a


list/tuple. Ans :
numbers = input("Enter numbers separated by space: ").split()
for i in range(len(numbers)):
numbers[i] = int(numbers[i])
largest = smallest = numbers[0]
for num in numbers[1:]:
if num > largest:
largest =
num elif num <
smallest:
smallest = num
print("Largest number:", largest)
print("Smallest number:",
smallest)

Q-19 Input a list of numbers and swap elements at the even location with the
elements at the odd location.
Ans :
val=eval(input("Enter a list "))
print("Original List is:",val)
s=len(val)
if s%2!=0:
s=s-1
for i in range(0,s,2):
val[i],val[i+1]=val[i+1],val[i]
print("List after swapping :",val)
Q-20 Input a list/tuple of elements, search for a given element in the
list/tuple. Ans :
elements = tuple(input("Enter elements separated by space: ").split())
search_element = input("Enter element to search: ")
if search_element in elements:
print(search_element, "is present in the tuple.")
else:
print(search_element, "is not present in the tuple.")

Q-21 Input a list of numbers and find the smallest and largest number from the
list. Ans :
values = input("Enter numbers separated by space: ")
numbers = values.split()
if numbers:
smallest = largest =
int(numbers[0]) for num in
numbers[1:]:
num = int(num)
if num < smallest:
smallest =
num
elif num >
largest:
largest = num
print("Smallest number:", smallest)
print("Largest number:", largest)
else:
print("No numbers entered.")

Q-22 Create a dictionary with the roll number, name and marks of n students in a
class and display the names of students who have scored marks above 75.
Ans :
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)
for student in result:
if result[student][1] > 75:
print("Students who got more than 75 marks are",(result[student][0]))

You might also like