12 Python Corrected PRG
12 Python Corrected PRG
num=int(input("enter a number"))
if(num==0):
fact=1
fact=1
for i in range(1,num+1):
fact=fact*i
print("factorial of",num,"is",fact)
Output
enter a number 5
factorial of 5 is 120
*************************************
prg1 b
output
Enter a value of n:5
The sum of the series is 701.0
***************************************
prg 2a
def oddeven(a):
if(a%2==0):
return 1
else:
return 0
num=int(input("enter a number:"))
if(oddeven(num)==1):
print("the given number is even")
elif(oddeven(num)==0):
print("the given number is odd")
output
enter a number:5
the given number is odd
****************************************
prg 2 b
def rev(str1):
str2=''
i=len(str1)-1
while i>=0:
str2+=str1[i]
i-=1
return str2
word=input("\n Enter a string:")
print("\n the mirror image of the given string is:",rev(word))
output
Enter a string: school
*************************************************
prg 3
num1=[]
for i in range(1,11):
num1.append(i)
print("numbers from 1 to 10.....\n", num1)
for j,i in enumerate(num1):
if(i%2==1):
del num1[j]
print("the values after removed odd numbers.....\n",num1)
output
numbers from 1 to 10.....
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
the values after removed odd numbers.....
[2, 4, 6, 8, 10]
******************************************************
prg 4
output:
888888888888888888888888888888888888888888
class String:
def __init__(self):
self.uppercase=0
self.lowercase=0
self.vowels=0
self.consonants=0
self.spaces=0
self.string=""
def getstr(self):
self.string=str(input("enter a string:"))
def count_upper(self):
for ch in self.string:
if(ch.isupper()):
self.uppercase+=1
def count_lower(self):
for ch in self.string:
if(ch.islower()):
self.lowercase+=1
def count_vowels(self):
for ch in self.string:
if(ch in('A','a','e','E','i','I','o','O','u','U')):
self.vowels+=1
def count_consonants(self):
for ch in self.string:
if(ch not in('A','a','e','E','i','I','o','O','u','U')):
self.consonants+=1
def count_space(self):
for ch in self.string:
if(ch==" "):
self.spaces+=1
def execute(self):
self.count_upper()
self.count_lower()
self.count_vowels()
self.count_consonants()
self.count_space()
def disply(self):
print("The given string contains...")
print("%d Uppercase letters"%self.uppercase)
print("%d lowercase letters"%self.lowercase)
print("%d vowels"%self.vowels)
print("%d consonants"%self.consonants)
print("%d spaces"%self.spaces)
S = String()
S.getstr()
S.execute()
S.disply()
output:
enter a string:Welcome to Computer Science
The given string contains...
3 Uppercase letters
21 lowercase letters
10 vowels
17 consonants
3 spaces