1.
Program to input two numbers and display the larger / smaller
number.
a= int(input(“Enter first number:”))
b = int(input(“Enter second number:”))
if a>b:
print(“First number is larger number”)
else:
print(“First number is smaller number”)
Output:
Enter first number:56
Enter second number:34
First number is larger number
Enter first number:34
Enter second number:56
First number is smaller number
2. Program to input three numbers and display the largest / smallest
number.
smallest=0
secondsmallest = 0
thirdsmallest = 0
lst = eval (input("Enter list of numbers"))
for number in lst: # 34,56,78
if number >smallest:
thirdsmallest = secondsmallest
secondsmallest = smallest
smallest = number
elif number >secondsmallest:
thirdsmallest = secondsmallest
secondsmallest = number
elif number >thirdsmallest:
thirdsmallest = number
print("smallest number is ",thirdsmallest)
Output:
Enter list of numbers56,34,90
smallest number is 34
3. Program to generate the following pattern using nested loop
4. Program to generate the following pattern using nested loop
1
5. Program to generate the following pattern using nested loop
6. Program to input the value of x and n and print the sum of the
following series:
1+x+x2+x3+x4+ ............xn
x = int(input(“Enter the value of x: “))
n = int(input(“Enter the value of n: “))
total = 1
multi = x
for i in range(1,n):
total = total + multi #sum
mu lti = multi * x #power
print(total)
7. Program to input the value of x and n and print the sum of the
following series:
1-x+x2-x3+x4+ ............xn
8. Program to input the value of x and n and print the sum of the
following series:
x + x2 +x3 + x4 + ........... xn
2 3 4 n
x = int(input(“Enter value of x”))
n = int(input(“Enter value of n”))
total = 1
for i in range(1, n):
total = total + ((x**i)/i)
print(total)
9. Program to determine whether a number is an armstrong number
or not
#Armstrong number - sum of its digits raised to the third power is
equal to the number itself.
For example 153 13+53+33 = 153 370 ,371,407
num= int (input(“Enter a number “))
sum = 0
temp = num
while temp>0:
2
digit = temp %10
sum + = digit **3 # calculate sum of cube of each digit
temp //=10
if num==sum: # sum compared with original number
print(“number is Armstrong number”)
else:
print ( “number is not Armstrong number “)
10. Program to determine whether a number is a palindrome or not
String = input(“Enter String”)
if String == String[ : : -1]:
print (“The string is a palindrome”)
else:
print(“The string is not a palindrome”)
or
string=input(“Enter a string:”)
length = len(string)
mid=int(length/2)
rev=-1
for a in range(mid):
if string[a] ==string[rev]:
a+=1
rev-=1
else:
print(string,”is not a palindrome”)
break
else:
print(string,”is a palindrome”)
or
num= int (input(“Enter a number “))
rev = 0
wnum = num
while (wnum>0):
digit = wnum %10
rev = rev*10 +digit
wnum=wnum//10
if num==rev:
print(“number is a palindrome”)
else:
3
print ( “number is not a palindrome “)
11.Program to determine whether a number is a perfect number or not
# Perfect number is a number which is equal to sum of its proper
divisors.
6 1,2 and 3
28 ,496
n = int(input("Enter any number: "))
sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if sum1 == n:
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")
12.Program to display Fibonacci series ,first 20 elements
Example:0,1,1,2,3,5,8,13
first=0
second=1
print(first)
print(second)
for a in range(1,19):
third= first+second
print(third,end= ‘ ‘)
first,second = second,third
13.Program to compute the greatest common divisor and least common
multiple of two integers.
x= int(input(“Enter first number:”))
y= int(input(“Enter second number:”))
if x>y:
smaller = y
else:
smaller=x
4
for i in range(1,smaller +1):
if (( x % i ==0) and(y % i==0)):
hcf =i
lcm=(x*y) /hcf
print(“HCF of ” ,x, ”and”, y, ”is”, hcf)
print(“LCM of “ ,x , “ and” ,y, ”is”, lcm)
14. Program to count and display uppercase and lowercase characters .
lcount=0
upcount=0
s=input("Enter a string")
l=len(s)
for i in range (0,l):
if s[i].isupper( ):
upcount+=1
else :
lcount+=1
print("uppercase letters :",upcount)
print("lowercase letters :",lcount)
15. Program to input a string and convert the case of characters in a
string and count and display the number of vowels, consonants
x=input(“Enter the string=”)
print(x)
v=0
c=0
lcl=0
ucl=0
for ch in x:
if(ch.islower( )):
lcl+=1
elif(ch.isupper( ) ):
ucl+=1
ch=ch.lower( )
if (ch is [“a”,”e”,”i”,”o”,”u”]):
v+=1
elif( ch is “b”,”c”,”d”,”f”,”g”,”h”,”j”,”k”,”l”,”m”,”n”,”p”,”q”,”r”,
”s”,”t”,”v”,”w”,”x”,”y”,”z”)
c+=1
print(“The number of vowels are=”,v)
print(“The number of consonants are =”,c)
5
print(“The number of lower case letters are =”,lcl)
print(“The number of upper case letters are =”,ucl)
16.
Term 2
16.Find the largest/smallest number in a list/tuple
val = eval(input(“Enter a list:”))
print(“The list is :”,val)
mx=max(val)
mn=min(val)
print(“Maximum number in the list :”,mx)
print(“Minimum number in the list :”,mn)
16.Input a list of numbers and swap elements at the even location with
the
elements at the odd location.
val =eval(input(“Enter a list:”))
print(“Original list is :”,val)
s=len(val)
if s% 2 !=0:
s=s-1
for i in range(0,s,2):
print(i,i+1))
val[i] , val[i+1] = val [i+1] , val[i]
print(“List after swapping:”,val)
Output:
Enter a list : [ 12 , 23 , 45 ,16,22,34]
Original list is : [ 12 , 23 , 45 , 16 , 22 , 34]
01
23
45
List after swapping : [ 23,12 , 16 , 45 , 34 ,22]
6
16. Input a list/tuple of elements, search for a given element in the
list/tuple.
lst = eval(input(“Enter list: “))
length = len(lst)
element = int(input(“Enter element to be searched for :”))
for i in range(0,length):
if element ==lst[i]:
print(element,”found at index”,i)
break
else:
print(element,”not found in given list”)
17.Input a list of numbers and test if a number is equal to the sum of the
cubes of its digits. Find the smallest and largest such number from the
given list of
numbers.
val=eval(input(“Enter a list:”))
alist = [ ]
s = len(val)
for i in range(s):
num = val[i]
csum = 0
while num:
dig = num% 10
csum += (dig * dig * dig)
num = num //10
if csum ==val[i]:
alist.append(val[i])
print(“Largest number from the given list is ”,max(alist))
print(“Smallest number from the given list is ”,max(alist))
18. Program to calculate the mean of a given list of numbers.
lst=eval(input(“Enter a list:”))
length = len(lst)
mean = sum=0
for i in range(0,length):
sum+=lst[i]
7
mean=sum/length
print(“Given list is :”,lst)
print(“The mean of the given list is ;” ,mean)
19. Program to count the frequency of a given element in a list of
numbers
lst=eval(input(“Enter list:”))
length = len(lst)
element=int(input(“Enter element:”))
count = 0
for i in range(0,length):
if element ==lst[i]:
count +=1
if count ==0:
print(element , “not found in given list”)
else:
print(element , “has frequency as “ , count , “in given list”)
Output:
Enter list : [1,1,1,2,2,3,4,2,2,5,5,2,2,5]
Enter element :2
2 has frequency as 6 in given list
20.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.
# Program to input a welcome message and display it.
a=input(“Enter your Welcome Message:”)
print(a)
Output:
Enter your Welcome Message:Hello Welcome
Hello Welcome
# Program to compute 𝑥n by taking two integers x and n from the
user.
import math
x=int(input(“Enter number 1:”))
n= int(input(“Enter number 2:”))
a = math.pow(x,n)
print(“x raise to the power n is”, a)
Output:
Enter number 1:5
8
Enter number 2:4
x raise to the power n is 625.0