CS Practical
CS Practical
Output:
Enter the value of n: 9
The sum of the sequence is: 165
s = hr + n
if s > 12:
s -= 12
Output:
Enter first side : 3
Enter second side : 5
Enter third side : 6
Triangle Possible
Q.6 Write a program that prompts the user for a strings and a
character c, and outputs the string produced from s by capitalizing
each occurrence of the character c in s and making all other
characters lowercase.
Source Code: -
def is_armstrong(n):
# Convert the number to a string
n_str = str(n)
# Calculate the sum of the cubes of each digit
sum_cubes = sum(int(digit)**3 for digit in n_str)
# Check if the sum of the cubes is equal to the original number
return sum_cubes == n
Output :
print(is_armstrong(153))
True
Q.14 Write a program to check if the elements in the first half of a
tuple are sorted in ascending order or not.
Source Code:
if
firsthalf_tuple == tuple(sorted(firthalf_tuple, reverse=True)):
Order is Descending
elif firsthalf_tuple == tuple(sorted(firsthalf_tuple)):
Order is not in descending order
else:
No order
Output:
t = (6, 5, 4, 3, 2, 1)
TRUE
Q.15 Write a program to calculate the area of an equilateral
triangle.
Source code:
import math
side = float(input("Enter side: "))
area = math.sqrt(3) / 4 * side * side
print("Area of triangle =", area)
Output:
Enter side: 5
Area of triangle = 10.825317547305481
Q.16 Write a program to find largest number of a list of numbers
entered through keyboard.
Source code:
print("Enter numbers:")
print("(Enter 'q' to see the result)")
l = input()
if l != 'q' and l != 'Q' :
l = int(l)
while True:
n = input()
if n == 'q' or n == 'Q' :
break
n = int(n)
if n > l :
l=n
print("Largest Number =", l)
Output:
Enter numbers:
(Enter 'q' to see the result)
3
5
8
2
4
q
Largest Number = 8
Q.17 Write a program to search for an element in a given list of
numbers.
Source code:
return largest
Output:
Input: a = 2, b = 4, c = 3
Output: 4