Computer Science Record Work
Computer Science Record Work
Source Code:
a = "Welcome"
print(a)
Output:
Program 2: Input two numbers and display the larger / smaller number.
Source Code:
Output:
Program 3: Input three numbers and display the largest / smallest number.
Source Code:
l = max(a,b,c)
s = min(a,b,c)
Output:
Program 4: Generate the following patterns using nested loops:
Source Code:
print("Pattern-1:")
for i in range(1, 6):
print("*" * i)
print("\nPattern-2:")
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
print("\nPattern-3:")
for i in range(1, 6):
for j in range(1, i + 1):
print(chr(64 + j), end="")
print()
Output:
Program 5: Write a program to input the value of x and n and print the sum of the
following series:
Source Code:
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
a=0
for i in range(n + 1):
a += x**i
print("Sum of Series 1:", a)
b=0
for i in range(n + 1):
if i % 2 == 0:
b += x**i
else:
b -= x**i
print("Sum of Series 2:", b)
c=0
for i in range(1, n + 1):
c += (x**i) / i
print("Sum of Series 3:", c)
d=0
f=1
for i in range(1, n + 1):
f *= i
d += (x**i) / f
print("Sum of Series 4:", d)
Output:
Program 6: Determine whether a number is a perfect number, an Armstrong
number or a palindrome.
Source Code:
p=0
temp = num
dig = len(str(num))
while temp > 0:
digit = temp % 10
p+= digit ** dig
temp //= 10
if p == num:
print(num ,"is an Armstrong Number.")
else:
print(num ,"is not an Armstrong Number.")
if str(num) == str(num)[::-1]:
print(num ,"is a Palindrome.")
else:
print(num ,"is not a Palindrome.")
Output:
Program 7: Input a number and check if the number is a prime or composite
number.
Source Code:
Output:
Program 8: Display the terms of a Fibonacci series.
Source Code:
Output:
Program 9: Compute the greatest common divisor and least common multiple of
two integers.
Source Code:
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
gcd = 1
for i in range(1, min(a, b) + 1):
if a % i == 0 and b % i == 0:
gcd = i
lcm = (a * b) // gcd
print("The GCD of", a, "and", b, "is:", gcd)
print("The LCM of", a, "and", b, "is:", lcm)
Output:
Program 10: Count and display the number of vowels, consonants, uppercase,
lowercase characters in string.
Source Code:
st = input("Enter a string: ")
v=0
c=0
U=0
l=0
vowels = "aeiouAEIOU"
for i in st:
if i.isalpha():
if i in vowels:
v += 1
else:
c += 1
if i.isupper():
U += 1
elif i.islower():
l += 1
print("Vowels:", v)
print("Consonants:", c)
print("Uppercase letters:", U)
print("Lowercase letters:", l)
Output:
Program 11: Input a string and determine whether it is a palindrome or not;
convert the case of characters in a string.
Source Code:
a= input("Enter a string: ")
if a.lower() == a[::-1].lower():
print(a, "is a Palindrome.")
else:
print(a, "is not a Palindrome.")
Output:
Program 12: Find the largest/smallest number in a list/tuple.
Source Code:
print(“Using List:”)
n = []
a = int(input("How many numbers do you want to enter? "))
for _ in range(a):
b = float(input("Enter a number: "))
n.append(b)
l = max(n)
s = min(n)
print(n)
print("Largest number in the list:", l)
print("Smallest number in the list:", s)
print(“Using Tuple:”)
n = []
a = int(input("How many numbers do you want to enter? "))
for _ in range(a):
b = float(input("Enter a number: "))
n.append(a)
t = tuple(n)
l = max(t)
s = min(t)
print(t)
print("Largest number in the tuple:", l)
print("Smallest number in the tuple:", s)
Output:
Program 13: Input a list of numbers and swap elements at the even location with
the elements at the odd location.
Source Code:
Output:
Program 14: Input a list/tuple of elements, search for a given element in the
list/tuple.
Source Code:
print("List:")
n = int(input("Enter the number of elements in the list: "))
l = []
for i in range(n):
a = input("Enter element: ")
l.append(a)
print(l)
s = input("Enter the element to search for: ")
if s in l:
print(s, "found at position", l.index(s) + 1)
else:
print(s, "not found in the list.")
print("Tuple:")
n =int(input("Enter the number of elements in the tuple: "))
l = []
for i in range(n):
a = input("Enter element: ")
l.append(a)
l = tuple(l)
print(l)
s = input("Enter the element to search for: ")
if s in l:
print(s, "found at position", l.index(s) + 1)
else:
print(s, "not found in the list.")
Outputs:
Program 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.
Source Code:
n = int(input("Enter the number of students: "))
d = {}
for i in range(n):
roll = input("Enter roll number: ")
name = input("Enter name: ")
marks = float(input("Enter marks: "))
d[roll] = {"name": name, "marks": marks}
Output: