Computer Science Practical Questions
Question 1 (Prime Number):
n=int(input("Enter a number:"))
c=0
for i in range(1,n+1):
if n%i==0:
c+=1
if c==2:
print("Prime Number")
else:
print("Not Prime")
Output:
Enter a number:79
Prime Number
Question 2 (Factorial):
s=int(input("Enter a number:"))
c=1
for i in range(2,s+1):
c*=i
print("Factorial of",s,"is",c)
Output:
Enter a number:8
Factorial of 8 is 40320
Question 3 (Fibonacci series):
print("*****Fibonacci Series*****")
a=int(input("Enter a range:"))
f=0
s=1
print(f,s,end=" ")
for i in range(2,a):
t=f+s
print(t,end=" ")
f,s=s,t
Output:
Enter a range:10
0 1 1 2 3 5 8 13 21 34
Question 4 (Armstrong Number):
print("*****Armstrong Number*****")
n=int(input("Enter a number :"))
s=0
st=str(n)
for i in st:
s+=int(i)**len(st)
if s==n:
print("Yes it is a armstrong number")
else:
print("No it is not armstrong number")
Output:
Enter a number :153
Yes it is a armstrong number
Question 5 (no. of uc, lc, vow, cons, digits):
s=input("Enter any sentence:")
u=l=v=c=d=0
for i in s:
if i.isalpha():
if i.isupper():
u+=1
else:
l+=1
if i in "aeiouAEIOU":
v+=1
else:
c+=1
elif i.isdigit():
d+=1
print("No. of Uppercase:",u)
print("No. of Lowercase:",l)
print("No. of Consonant:",c)
print("No. of Vowels:",v)
print("No. of Digits:",d)
Output:
Enter any sentence: Computer Science 2024-25
No. of Uppercase: 2
No. of Lowercase: 13
No. of Consonant: 9
No. of Vowels: 6
No. of Digits: 6
Question 6 (words starting with vowels):
s=input("Enter a sentence:")
l=s.split()
c=0
for i in l:
if i[0] in "aeiouAEIOU":
c+=1
print("Words starting with vowels are:",c)
Output:
Enter a sentence: I am an Iconic star in the Universe
Words starting with vowels are: 6
Question 7 (even to half, odd to twice in a list):
l=eval(input("Enter a List:"))
for i in range(len(l)):
if l[i]%2==0:
l[i]//=2
else:
l[i]*=2
print(l)
Output:
Enter a List: [7,18,45,10,3,9,24,23]
[14, 9, 90, 5, 6, 18, 12, 46]
Question 8 (addition of ASCII values of letters in a word):
s=input("Enter a sentnece:")
d=s.split()
for i in d:
t=0
for j in i:
t+=ord(j)
print(i,t,sep="-")
Output:
Enter a sentnece: Hi I am a Hacker
Hi-177
I-73
am-206
a-97
Hacker-590
Question 9 (to make a list containing index of even digits from a list):
l=[1,2,3,4,5,6,7,8,9,10]
indexl=[]
for i in range(len(l)):
if l[i]%2==0:
indexl.append(i)
print(indexl)
Output:
[1, 3, 5, 7, 9]
Question 10 (nested dictionary):
d={"Dhoni":{"1st Match":77,"2nd Match":42,"3rd Match":45},
"Virat":{"1st Match":56,"2nd Match":68,"3rd Match":37},
"Rohit":{"1st Match":67,"2nd Match":52,"3rd Match":40}}
for i in d:
print(i)
s=0
for j in d[i]:
print(j,d[i][j])
s+=d[i][j]
print("Total:",s)
print("*"*10)
Output:
Dhoni
1st Match 77
2nd Match 42
3rd Match 45
Total: 164
**********
Virat
1st Match 56
2nd Match 68
3rd Match 37
Total: 161
**********
Rohit
1st Match 67
2nd Match 52
3rd Match 40
Total: 159
**********