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

Computer Science Record Work

The document contains a series of Python programs that perform various tasks such as displaying messages, comparing numbers, generating patterns, calculating series sums, and checking properties of numbers. Each program includes source code and expected output, covering topics like prime numbers, Fibonacci series, GCD/LCM calculations, and string manipulations. Additionally, it demonstrates how to work with lists, tuples, and dictionaries in Python.

Uploaded by

mailpravesh09
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)
2 views

Computer Science Record Work

The document contains a series of Python programs that perform various tasks such as displaying messages, comparing numbers, generating patterns, calculating series sums, and checking properties of numbers. Each program includes source code and expected output, covering topics like prime numbers, Fibonacci series, GCD/LCM calculations, and string manipulations. Additionally, it demonstrates how to work with lists, tuples, and dictionaries in Python.

Uploaded by

mailpravesh09
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/ 23

Program 1: Input a welcome message and display it.

Source Code:

a = "Welcome"
print(a)

Output:
Program 2: Input two numbers and display the larger / smaller number.

Source Code:

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


b= int(input("Enter the second number: "))
if a > b:
print("The larger number is:",a)
print("The smaller number is:",b)
elif a < b:
print("The larger number is:",b)
print("The smaller number is:",a)
else:
print("Both numbers are equal.")

Output:
Program 3: Input three numbers and display the largest / smallest number.

Source Code:

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


b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))

l = max(a,b,c)
s = min(a,b,c)

print("The largest number is:", l)


print("The smallest number is:", s)

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:

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


d=0
for i in range(1, num):
if num % i == 0:
d += i
if d == num:
print(num ,"is a Perfect Number.")
else:
print(num ,"is not a Perfect Number.")

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:

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


if n < 2:
print(n ,"is neither Prime nor Composite.")
else:
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
print(n, "is a Composite Number.")
break
else:
print(n, "is a Prime Number.")

Output:
Program 8: Display the terms of a Fibonacci series.

Source Code:

n = int(input("Enter the number of terms: "))


a, b = 0, 1
print("Fibonacci Series:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b

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:

n = int(input("Enter the number of elements: "))


no = []
for i in range(n):
a = int(input("Enter element: "))
no.append(a)

for i in range(0, len(no) - 1, 2):


no[i], no[i + 1] = no[i + 1], no[i]
print("Swapped list:", no)

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}

print("\nStudents with marks above 75:")


for roll, q in d.items():
if q["marks"] > 75:
print(q["name"])
print("\nStudents with marks below 75:")
for roll, q in d.items():
if q["marks"] < 75:
print(q["name"])

Output:

You might also like