0% found this document useful (0 votes)
33 views20 pages

Ethan's Computer Practicals File 11-B

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

Ethan's Computer Practicals File 11-B

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

Computer Science Practical –

Ethan Vernon Lasrado

1. Input a welcome message and display it.

Code:
message = input("Enter the message: ")
print(message)

Output:

Enter the welcome message: Hello World


Hello World
2. Input two numbers and display the larger / smaller number

Code:

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))

if num1 > num2:


larger = num1
smaller = num2
elif num2 > num1:
larger = num2
smaller = num1
else:
print("Both numbers are equal.")
exit()

print(f"Larger number: {larger}")


print(f"Smaller number: {smaller}")

Output:

Enter the first number: 55


Enter the second number:88
Larger number: 88.0
Smaller number: 55.0
3. Input three numbers and display the largest / smallest
number.

Code:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

if num1 > num2 and num1 > num3:


larger = num1
if num2 > num3:
smaller = num3
elif num2 > num1 and num2 > num3:
larger = num2
if num1 > num3:
smaller = num3
elif num3 > num2 and num3 > num1:
larger = num3
if num2 > num1:
smaller = num1
else:
print("All numbers are equal.")
exit()

print(f"Larger number: {larger}")


print(f"Smaller number: {smaller}" )

Output:

Enter the first number:1


Enter the second number:2
Enter the third number:3
Larger number: 3.0
Smaller number: 1.0
4. Generate the following patterns using nested loops:

Code: Pattern-1

for i in range(0,6):
for j in range(1,i+1):
print("*",end='')
print()

Output:

*
**
***
****
*****
4. Generate the following patterns using nested loops:

Code: Pattern-2

for i in range(5,0,-1):
for j in range(1,i+1):
print(j,end='')
print()

Output:

12345
1234
123
12
1
4. Generate the following patterns using nested loops:

Code: Pattern-3

Alphabets = "ABCDE"
n = len(Alphabets)

for i in range(n):
for a in range(i + 1):
print(Alphabets[a], end='')
print(" ")

Output:

A
AB
ABC
ABCD
ABCDE
5. Write a program to input the value of x and n and print
the sum of the following series:

Code: Series-1

number = int(input("Enter a value: "))


number_end = int(input("Enter a value: "))
total = 1
for i in range(0,number_end):
total+=number**i
print(f"Therefore the sum of the given series is
{total}")

Output:

Enter a value: 1
Enter a value: 1
Therefore the sum of the given series is 2
5. Write a program to input the value of x and n and print
the sum of the following series:

Code: Series-2

x = int(input("Enter the value of x: "))


n = int(input("Enter the value of n: "))

total = 0

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


term = (-1) ** i * x ** i
total += term

print("The end value of the series is:", total)

Output:

Enter a value: 1
Enter a value: 1
Therefore the sum of the given series is 0
6. Determine whether a number is a perfect number, an
Armstrong number or a palindrome.:

Code:

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


sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
Perfect_Number=True
else:
Perfect_Number=False

sum = 0
n1 = len(str(n))
temp = n
while temp > 0:
digit = temp % 10
sum += digit ** n1
temp //= 10
if n == sum:
Armstrong=True
else:
Armstrong=False

rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
Palindrome=True
else:
Palindrome=False

if Perfect_Number==True and Armstrong==True and


Palindrome==True:
print("The number is a perfect, Armstrong and
Palindrome number")
elif Perfect_Number==True and Armstrong==True and
Palindrome==False:
print("The number is a perfect and Armstrong
number")
elif Perfect_Number==True and Armstrong==False and
Palindrome==True:
print("The number is a perfect and Palindrome
number")
elif Perfect_Number==False and Armstrong==True and
Palindrome==True:
print("The number is a Armstrong and Palindrome
number")
elif Perfect_Number==True and Armstrong==False and
Palindrome==False:
print("The number is a Perfect number")
elif Perfect_Number==False and Armstrong==True and
Palindrome==False:
print("The number is a Armstrong number")
elif Perfect_Number==False and Armstrong==False and
Palindrome==True:
print("The number is a Palindrome number")
elif Perfect_Number==False and Armstrong==False and
Palindrome==False:
print("The number is not any of the above")
Output:

Enter any number: 6


The number is a perfect, Armstrong and Palindrome number
7. Write a program to check whether the given number is a
prime or composite number

Code:

num = int(input("Enter a number: "))


prime=True

if num == 1:
print(num, "is not a prime number")
elif num > 1:
for i in range(2, num):
if (num % i) == 0:
prime=False
break;
if prime:
print(num, "is a prime number")
else:
print(num, "is not a prime number")

Output:

Enter a number: 7
7 is a prime number
8. Display the terms of a Fibonacci series.

Code:

n = int(input("Enter the number of time it should


repeat: "))

term1 = 0
term2 = 1

print(term1)
print(term2)

for i in range(2, n):


next_term = term1 + term2
print(next_term)
term1 = term2
term2 = next_term

Output:

0
1
1
2
3
5
8
13
21
34
9. Compute the greatest common divisor and least common
multiple of two integers

Code:

num1 = int(input("Enter the first number: "))


num2 = int(input("Enter the second number: "))
hcf = 1

for i in range(1, min(num1, num2)):


if num1 % i == 0 and num2 % i == 0:
hcf = i
lcm = int((num1*num2)/hcf)
print("The HCF of {num1} and {num2} is {hcf}, the LCMis
{lcm}")

Output:

Enter the first number: 5


Enter the second number: 10

The HCF of 5 and 10 is 5, the LCM is 10


10. Count and display the number of vowels, consonants,
uppercase, lowercase characters in string

Code:

Uppercase = ["A", "B", "C", "D", "E", "F", "G", "H","I", "J", "K",
"L", "M", "N", "O", "P", "Q","R", "S", "T", "U", "V", "W", "X",
"Y", "Z"]
x = str(input("Enter a string: "))
num_vowel=0
num_cons=-1
num_upper=0
num_lower=-1
for i in x:
if i in "aeiouAEIOU":
num_vowel+=1
else:
num_cons+=1
if i in Uppercase:
num_upper+=1
else:
num_lower+=1

print(f"The string {x} has {num_vowel} vowels. It has {num_cons}


consonants. It has {num_upper} uppercase letters. It has
{num_lower} lowercase letters")

Output:

Enter a string: Python


The string Hello World has 1 vowels. It has 5
consonants. It has 1 uppercase letters. It has 5
lowercase letters
11. Input a string and determine whether it is a
palindrome or not; convert the case of characters in a
string.
Code:

str=input("Enter a string:")
w=""
for element in str[::-1]:
w = w+element

if (str==w):
print(str, 'is a Palindrome string')
else:
print(str,' is NOT a Palindrome string')

Output:

Enter a string: “kanak”


Kanak is a Palindrom string
12. Find the largest/smallest number in a list/tuple

Code:

list1 = [7, 10, 12, 3, 100, 95]


list1.sort()
print("Smallest element is:", list1[:1])

Output:

Smallest element is: 3


13. Input a list of numbers and swap elements at the even
location with the elements at the odd location.

Code:

mylist = []

print("Enter 5 elements for the list: ")

for i in range(5):

value = int(input())

mylist.append(value)

print("The original list : " + str(mylist))

odd_i = []

even_i = []

for i in range(0, len(mylist)):

if i % 2:

even_i.append(mylist[i])

else :

odd_i.append(mylist[i])

result = odd_i + even_i

print("Separated odd and even index list: " + str(result))

Output:

Enter 5 elements for the list:

4
7
16
7
9

The original list: [4,7,16,7,9]

Seperated odd and even index list: [4,16,9,7,7]


14. Input a list/tuple of elements, search for a given
element in the list/tuple.

Code:

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)

Output:

Enter 5 elements for the list:


45
34
37
34
35

Enter an element to be searched:


34

Element found at index: 1


Element found at position: 2
15. Input a list of numbers and test if a number is equal
to the sum of the cubes of its digits. Find the smallest
and largest such number from the given list of numbers.

Code:

print("Program to Input a list of numbers and test if a


number is equal to the sum of the cubes of its digits. \n")
print("Find the smallest and largest such number from the
given list of numbers.")
list=[]
list1=[]
num=int(input("How many Numbers in the list: "))
for n in range(num):
numbers=int(input('Enter Number'))
list.append(numbers)
print("The Entered list is ",list)
for i in range(num):
sum=0
temp = list[i]
order = len(str(list[i]))
while temp (less) 0:
digit = temp % 10
k=digit**order
sum = sum+k
temp //= 10
if list[i] == sum:
list1.append(list[i])
print("the largest number is:",max(list1))
print("the smallest number is:",min(list1))
16. 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.

Code:

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("Student's name who get more than 75 marks


is/are",(result[student][0]))

Output:

Enter number of students: 4


Enter Details of Students No: 1
Roll No: 1
Student Name: Amit
Marks: 78

Enter Details of students No: 2


Roll No: 2
Student Name: Abhay
Marks: 71

Enter Details of students No: 2


Roll No: 3
Student Name: Pooja
Marks: 70

Enter Details of students No: 3


Roll No: 4
Student Name: Aarti
Marks: 60

Student’s name who get more than 75 marks is/are Amit

You might also like