Professional Documents
Culture Documents
College of Technology and Engineering, Mpuat, Udaipur (Raj.)
College of Technology and Engineering, Mpuat, Udaipur (Raj.)
)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI
PROGRAM-19
OBJECTIVE - Write a program to find weather the number is positive of negative using
ladder if - else
CODE –
num =float(input("Enter the number : "))
if num>0:
print(num," is a positive number")
elif num==0:
print(num," is a zero intiger")
else:
print(num," is a negative number")
OUTPUT :-
PROGRAM-20
OBJECTIVE - Write a program to find weather the number is positive of negative using
nested if - else
CODE –
num =float(input("Enter the number : "))
if num>0:
if num==0:
print(num," is a zero intiger")
else:
print(num," is a positive number")
else:
print(num," is a negative number")
OUTPUT :-
PROGRAM-21
CODE –
# print factors of a number
def factors(x):
for i in range(1,x+1):
if(x%i==0):
print(i)
num=int(input("enter the number : "))
factors(num)
OUTPUT :-
PROGRAM-22
CODE –
# print calculator
def addition(x,y):
return x+y
def sub(x,y):
return x-y
def multi(x,y):
return x*y
def divide(x,y):
return x/y
if choice in [1,2,3,4]:
num1=float(input("enter first number : "))
num2=float(input("enter 2nd number : "))
if choice==1:
print(" addition is : ",addition(num1,num2))
elif choice == 2:
print(" subtraction is : ", sub(num1, num2))
elif choice == 3:
print(" multipli is : ", multi(num1, num2))
else:
print(" divide is : ", divide(num1, num2))
else:
print("wrong input")
next_calculation=input("want to do next calculation (yes/no)? ")
if(next_calculation=="no"):
break
OUTPUT :-
PROGRAM-23
CODE –
def calculate_lcm(x,y):
if(x>y):
greater=x
else:
greater=y
while True:
if((greater%x==0)and(greater%y==0)):
lcm=greater
break
greater+=1
return lcm
num1=int(input("enter first number : "))
num2=int(input("enter 2nd number : "))
print("lcm of numbers is : ",calculate_lcm(num1,num2))
OUTPUT :-
PROGRAM-24
CODE –
# power of 2 using anonymous funciton
terms=int(input("enter the range of numbers : "))
result=list(map(lambda x:2**x,range(terms)))
for i in range(terms):
print("2 raised to power ",i," is ",result[i])
OUTPUT :-
PROGRAM-25
CODE –
# Python program to display the Fibonacci sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
OUTPUT :-
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI
PROGRAM-26
CODE –
X = [[12,7],
[4 ,5],
[3 ,8]]
result = [[0,0,0],
[0,0,0]]
for r in result:
print(r)
OUTPUT :-
[12, 4, 3]
[7, 5, 8]
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI
PROGRAM-27
OBJECTIVE - Write a program to sort alphabetically word form a string provided by the
user.
CODE –
my_str = input("Enter a String : ")
words = [word.lower() for word in my_str.split()]
words.sort()
print("The sorted words are:")
for word in words:
print(word)
OUTPUT :-
Enter a String : My name is sourabh manawat
The sorted words are:
is
manawat
my
name
sourabh
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI
PROGRAM-28
OBJECTIVE - Write a program to find the sum of natural numbers using Recursion.
CODE –
def recur_sum(n):
if n <= 1:
return n
else:
return n + recur_sum(n-1)
if num < 0:
print("Enter a positive number")
else:
print("The sum is",recur_sum(num))
OUTPUT :-
Enter the Number : 20
The sum is 210
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI
PROGRAM-29
CODE –
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
OUTPUT :-
Enter the number : 10
The factorial of 10 is 3628800
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI
PROGRAM-30
CODE –
def convertToBinary(n):
if n > 1:
convertToBinary(n//2)
print(n % 2,end = '')
convertToBinary(dec)
print()
OUTPUT :-
PROGRAM-31
CODE –
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = input("Enter a string : ");
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
OUTPUT :-
PROGRAM-32
CODE –
# str1 = "Race"
# str2 = "Care"
str1=input("Enter 1st string : ")
str2=input("Enter 2nd string : ")
# convert both the strings into lowercase
str1 = str1.lower()
str2 = str2.lower()
OUTPUT :-
Enter 1st string : race
Enter 2nd string : care
race and care are anagram.
COLLEGE OF TECHNOLOGY AND ENGINEERING, MPUAT, UDAIPUR (RAJ.)
Name – SOURABH MANAWAT Class – B. Tech, III Year, CSE Sem – VI
PROGRAM-33
CODE –
def outer_function():
num = 20
def inner_function():
global num
num = 25
outer_function()
print("Outside both function: ", num)
OUTPUT :-
PROGRAM-34
CODE –
# import standard math module
import math
OUTPUT :-