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

Chapter 1

Uploaded by

sindudeepika347
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Chapter 1

Uploaded by

sindudeepika347
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNSOLVED QUESTIONS – CHAPTER 3

1.Write a program to print all-natural numbers in reverse (from n to 1).


n=int(input(‘enter the n value’))
for i in range(n,0,-1):
print(i)

2.Write a program to print all even numbers up to n.


n=int(input('enter the n value'))
for i in range(1,n+1,1):
if(i%2==0):
print(i)
OR
n=int(input(‘enter a number’))
i=1
while(i<=n):
if(i%2==0):
print(i)
i=i+1
3.Write a program to print sum of all odd numbers between two intravel given.
n1=int(input('initial value of the interval'))
n2=int(input('final value of the interval'))
sum=0
for i in range(n1,n2+1):
if(i%2!=0):
sum=sum+i
print(sum)

4. Write a program to print table of any number.


t=int(input('table number'))
n=int(input('upto the multiple(number)'))
for i in range(1,n+1,1):
print(t,'x',i,'=',(t*i))

5. Write a program to enter any number and calculate sum of its digits.
n=int(input('enter a number'))
sum=0
while(n!=0):
remainder=n%10
sum=sum+remainder
n=n//10
print(sum)

6. Write a program to print all Prime numbers between 1 to n.


a=int(input('enter last number of interval'))
for n in range(1,a+1):
c=0
for i in range(1,n+1):
if(n%i==0):
c=c+1

Prepared by M.Kiran Kumar, CT02


if(c==2):
print(n)
7. Write a program to enter any number and display perfect numbers between 1 to n.
a=int(input('enter the last number'))
for n in range(1,a):
sum=0
for i in range(1,n):
if(n%i==0):
sum=sum+i
if(sum==n):
print(sum)
8. Write a program to enter any number and find its first and last digit.
n=int(input('enter a number'))
print('last digit',n%10)
while(n>10):
n=n//10
print('first digit',n)

9. Write a program to display given number is a number palindrome or not.


n1=n
reverse=0
while(n!=0):
remainder=n%10
reverse=(reverse*10)+remainder
n=n//10
if(n1==reverse):
print('it is a palindrome ')
else:
print('It is not a palindrome')
10. Write a program to print all alphabets from a to z.
n=97
for i in range(1,27):
print(chr(n))
n=n+1
11. Write a program to enter any number and check whether it is Armstrong number or not.
n=int(input('enter a number'))
n1=n
arm=0
l=len(str(n))
while(n!=0):
remainder=n%10
arm=arm+(remainder**l)
n=n//10
if(arm==n1):
print('it is a armstrong number')
else:
print('it is not an armstrong number')

12. Write a program to print all Strong numbers between 1 to n.


a=int(input('enter last number'))

Prepared by M.Kiran Kumar, CT02


for n in range(1,a):
n1=n
strong=0
while(n!=0):
remainder=n%10
factorial=1
for i in range(1,remainder+1):
factorial=factorial*i
strong=strong+factorial
n=n//10

if(n1==strong):
print('strong number is',strong)
13. Write a program to print Fibonacci series up to n terms.
a=0
b=1
print(a,b,end=',')
n=int(input('enter a number'))
for i in range(1,n):
c=a+b
print(c,end=',')
a=b
b=c
14. Star pattern programs - Write a program to print the given star patterns.
n=6
for i in range(1,n):
for j in range(0,i):
print('*',end=' ')
print()

n=6
for i in range(1,n):
for j in range(0,i):
print('*',end=' ')
print()
for i in range(n,0,-1):
for j in range(0,i):
print('*',end=' ')
print()

n=6
for i in range(0,n):
print(' '*(n-i-1)+'* '*(i+1))
for i in range(0,n):
print(' '*(i+1)+'* '*(n-i-1))

Prepared by M.Kiran Kumar, CT02


15. Write a Python program to construct the following patterns, using a nested loop number

Prepared by M.Kiran Kumar, CT02


UNIT-5:STRINGS & DICTIONARIES

1.Write a program to find number of digits ,alphabets and symbols ?


s=input('enter a string')
digits,alphabets,symbols=0,0,0
for i in s:
if i.isdigit():
digit=digit+1
elif i.isalpha():
alphabets=alphabets+1
else:
symbols=symbols+1 #includes spaces
print(digits,alphabets,symbols)

2.Write a program to convert lower case to upper case from given string?
s=input('string')
s1=''
for i in s:
s1=s1+chr(ord(i)-32)
print(s1)

3.Write a program to print the following output?


s='RGUKT'
for i in range(len(s)+1):
for j in range(i):
print(s[j],end=' ')
print()
for i in range(len(s)-1,0,-1):
for j in range(i):
print(s[j],end=' ')
print()

4. Write a program to check whether given string is palindrome or not?


s=input('enter a string')
rev=''
for i in s:
rev=i+rev
if rev==s:
print(s,'is a palindrome')
else:
print(s,'is not a palindrome')

5. Write a program to find no_ words, no_letters, no_digits and no_blanks in a line?
s=input('enter a line')
w,l,d,b=0,0,0,0
k=s.split()
w=w+len(k)
for i in s:

Prepared by M.Kiran Kumar, CT02


if i.isalpha():
l=l+1
if i.isdigit():
d=d+1
if i.isspace():
b=b+1
print(w,l,d,b)

6. Write a program to sort list names in alphabetical order?


s='kiran kumar ram anjali pavani'
k=s.split()
c=sorted(k)
print(c)

7. To find the first character from given string, count the number of times repeated and
replaced with * except first character then print final string?
s='kiran kumar kiran '
print(s.count(s[0]))
print(s[0]+s[1:].replace(s[0],'*'))

8. To find the strings in a list which are matched with first character equals to last character
in a string?
l=['kiran','harish','did','do']
for i in l:
if i[0]==i[-1]:
print(i)

9. Write a program that accepts a string from user and redisplays the same string after
removing vowels from it?
s=input('enter a string containing vowels')
for i in s:
if i not in ['a','e','i','o','u','A','E','I','O','U']:
print(i,end='')

10. This is a Python Program to take in two strings and display the larger string without using
built-in functions.?
s1=input('string 1')
s2=input('string 2')
c1,c2=0,0
for i in s1:
c1=c1+1
for i in s2:
c2=c2+1
if c1>c2:
print(s1)
else:
print(s2)

Prepared by M.Kiran Kumar, CT02


11. Python Program to Read a List of Words and Return the Length of the Longest One?

s='kiran kumar harish yogesh '


l=s.split()
maxx=l[0]
for i in l:
if(len(i)>len(maxx)):
maxx=i
print(maxx)

12. Python Program to Calculate the Number of Upper-Case Letters and Lower-Case Letters
in a String?
s='VGb chgkJM cgLBJ yggJSZKGB B'
l,u=0,0
for i in s:
if i.islower():
l=l+1
elif i.isupper():
u=u+1
print(l,u)
OR
c1=0
c2=0
for i in s:
if i<='z' and i>='a':
c1=c1+1
elif i>='A' and i<='Z':
c2=c2+1
print('small',c1,'capital',c2)

13. Write a python program to multiply all the items in a dictionary.


d={1:'q',2:'k',3:'r'}
m=1
for i in d:
m=m*i*(ord(d[i]))
print(m)

14. Python Program to Create a Dictionary with Key as First Character and Value as Words
Starting with that Character
d={}
s='sxwvbnj ahhhah ajkb ahjnvc svghnvf kkk'
l=s.split()
for word in l:
if word[0] not in d.keys():
d[word[0]]=[]
d[word[0]].append(word)
else:
if word[0] in d.keys():

Prepared by M.Kiran Kumar, CT02


d[word[0]].append(word)
print(d)

Prepared by M.Kiran Kumar, CT02


UNIT_2:Data types, I/O, Types of Errors and Conditional Constructs

Solved Example Problems

1. Write a Python program to find whether the given number is divisible by 7 and multiple of
5?
n=int(input('enter a number'))
if n%7==0 and n%5==0:
print(n,'the given number is divisible by 7 and multiple of 5')
else:
print(n,'the given number is not divisible by either 7 or the multiple of 5')

2. Write a program to input any number and check whether it is even or odd
n=int(input('enter a number'))
if n%2==0:
print(n,'is a even number')
else:
print(n,'is a odd number')

3. Write a program to input any number and check whether it is negative, positive or zero
n=int(input('enter a number'))
if n>0:
print(n,'is a positive number')
elif(n<0):
print(n,'is a negative number')
else:
print('zero')

4. A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended. And print
percentage of class attended
and student is allowed to sit in exam or not.
nch=int(input('no of classes held'))
nca=int(input('no. of classes attended'))
p=((nch-nca)/nch)*100
a=100-p
print(a)
if a>=75:
print('student is allowed to write exams')
else:
print('student is not allowed to write exams')

Prepared by M.Kiran Kumar, CT02


UNSOLVED PROBLEMS

1. Take values of length and breadth of a rectangle from user and check if it is square or not.
l=int(input('enter length'))
b=int(input('enter breadth'))
if l==b:
print('it is a square')
else:
print('it is not a square')

2. Take two int values from user and print greatest among them
a=int(input('enter a number'))
b=int(input('enter a number'))
if a>b:
print('greatest number=',a)
else:
print('greatest number',b)

3. A shop will give discount of 10%, if the cost of purchased quantity is more than 1000.
Ask user for quantity Suppose, one unit will cost 100.
Ex: quantity=11
cost=11*100=1100>1000 so, he will get 10% discount, that is 110.
Final cost is 1100-110=990
q=int(input('enter quanitity'))
cost=q*100
d=(cost/100)*10
if cost>1000:
print('final cost is',1000-d)
else:
print('final cost is ',costs)

4. A company decided to give bonus of 5% to employee if his/her year of service is more than 5
years.Ask user for their salary and year of service and print the net bonus amount.

5. A school has following rules for grading system:


a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
m=int(input('enter marks'))
if m>80:
pritn('A')
elif m<=80 and m>60:
print('B')
elif m<=60 and m>50:
print('C')
elif m<=50 and m>45:
print('D')
elif m<=45 and m>25:
print('E')

Prepared by M.Kiran Kumar, CT02


elif m<=25 and m>=0:
print('F')
else:
print('invalid data')

6. Take input of age of 3 people by user and determine oldest and youngest among them.
a1=int(input('age of 1st person'))
a2=int(input('age of 2nd person'))
a3=int(input('age of 3rd person'))
if a1>a2 and a1>a3:
print('oldest',a1)
elif a2>a1 and a2>a3:
print('oldest',a2)
else:
print('oldest',a3)
if a1<a2 and a1<a3:
print('youngest',a1)
elif a2<a1 and a2<a3:
print('youngest',a2)
else:
print('youngest',a3)

7. Write a program to print absolute vlaue of a number entered by user.


E.g.-
INPUT: 1
OUTPUT: 1
INPUT: -1
OUTPUT: 1
n=int(input('enter a number'))
if n>=0:
print('absolute value=',n)
else:
print('absolute value=',((-1)*(n)))

8. Modify the 4th question (in solved problem set) to allow student to sit if he/she has medical
cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.
nch=int(input('no of classes held'))
nca=int(input('no. of classes attended'))
mc=input('enter Y or N if you have medical cause(y-yes,N-No)')
p=((nch-nca)/nch)*100
a=100-p
print(a)
if a>=75:
print('student is allowed to write exams')
elif(mc=='Y'):
print('student is allowed to write exams')
else:
print('student is not allowed to write exams')

Prepared by M.Kiran Kumar, CT02


9. Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules
print their place
of service.
if employee is female, then she will work only in urban areas.
if employee is a male and age is in between 20 to 40 then he may work in anywhere
if employee is male and age is in between 40 t0 60 then he will work in urban areas only.
And any other input of age should print "ERROR".
sex=input('enter your gender M or F ,(M-male,F-Female)')
maritial_status=input('enter your maritial stattus Y or N(Y-yes,N-No)')
age=int(input('enter your age'))
if sex=='F':
print('she will work only in urban areas. ')
elif (sex=='M' and (age>20 and age<40)):
print('he may work in anywhere')
elif(sex=='M' and (age>=40 and age<60)):
print('he will work in urban areas only.')
else:
print('ERROR')

Prepared by M.Kiran Kumar, CT02

You might also like