0% found this document useful (0 votes)
26 views4 pages

12 Python Corrected PRG

The document contains 5 Python programs. Program 1 calculates the factorial of a number input by the user. Program 2 determines whether a number is even or odd. Program 3 removes odd numbers from a list of numbers 1-10. Program 4 performs set operations on odd and prime numbers. Program 5 counts the number of uppercase letters, lowercase letters, vowels, consonants, and spaces in a string input by the user.

Uploaded by

Akshaya Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

12 Python Corrected PRG

The document contains 5 Python programs. Program 1 calculates the factorial of a number input by the user. Program 2 determines whether a number is even or odd. Program 3 removes odd numbers from a list of numbers 1-10. Program 4 performs set operations on odd and prime numbers. Program 5 counts the number of uppercase letters, lowercase letters, vowels, consonants, and spaces in a string input by the user.

Uploaded by

Akshaya Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

prg 1 a

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

n=int(input("Enter a value of n:"))


s=0.0
for i in range(1,n+1):
a=float(i**i)/i
s=s+a
print("The sum of the series is ",s)

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

the mirror image of the given string is: loohcs

*************************************************

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

odd=set([x*2+1 for x in range(0,5)])


primes=set()
for i in range(2,10):
j=2
f=0
while j<=i/2:
if i%j==0:
f=1
j+=1
if f==0:
primes.add(i)
print("Odd Numbers:",odd)
print("Prime Numbers:",primes)
print("Union:",odd.union(primes))
print("Intersection:",odd.intersection(primes))
print("Difference:",odd.difference(primes))
print("Symmetric Difference:",odd.symmetric_difference(primes))

output:

Odd Numbers: {1, 3, 5, 7, 9}


Prime Numbers: {2, 3, 5, 7}
Union: {1, 2, 3, 5, 7, 9}
Intersection: {3, 5, 7}
Difference: {1, 9}
Symmetric Difference: {1, 2, 9}

888888888888888888888888888888888888888888

python 5th program:

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

You might also like