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

Python Program

The document contains code snippets demonstrating various arithmetic, string, comparison, and mathematical operations in Python including: 1) Taking user input to perform addition, subtraction, multiplication, division and other arithmetic operations on two numbers 2) Concatenating two user-input strings and printing a substring 3) Comparing three user-input numbers to find the greatest value 4) Converting between Celsius and Fahrenheit temperatures 5) Defining a recursive function to calculate the factorial of a user-input number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Python Program

The document contains code snippets demonstrating various arithmetic, string, comparison, and mathematical operations in Python including: 1) Taking user input to perform addition, subtraction, multiplication, division and other arithmetic operations on two numbers 2) Concatenating two user-input strings and printing a substring 3) Comparing three user-input numbers to find the greatest value 4) Converting between Celsius and Fahrenheit temperatures 5) Defining a recursive function to calculate the factorial of a user-input number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Arithmetic operations:

num1 = int(input('Enter First number: '))


num2 = int(input('Enter Second number '))
add = num1 + num2
dif = num1 - num2
mul = num1 * num2
div = num1 / num2
floor_div = num1 // num2
power = num1 ** num2
modulus = num1 % num2
print('Sum of ',num1 ,'and' ,num2 ,'is :',add)
print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)
print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
print('Division of ',num1 ,'and' ,num2 ,'is :',div)
print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div)
print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)
print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)

Output :

String concatenation:

s1=input("enter the string1")

s2=input("enter the string2")

s3=s1+s2

print("concatenated string :")


print(s3)

print("sub string is:")

print(s1[1:])

enter the string1:hello


enter the string2:world
concatenated string :
helloworld
sub string is:
ello

greatest among 3 numbers:

num1=int(input("Enter the number1:"))

num2=int(input("Enter the number2:"))

num3=int(input("Enter the number3:"))

if (num1 >= num2) and (num1 >= num3):

largest = num1

elif (num2 >= num1) and (num2 >= num3):

largest = num2

else:

largest = num3

print("The largest number between",num1,",",num2,"and",num3,"is",largest)

Enter the number1:5


Enter the number2:10
Enter the number3:6
The largest number between 5 , 10 and 6 is 10
Temperature conversion:

celsius=float(input("Enter the temperature in celcius:"))

farenheit=float(input("Enter the temperature in farenheit:"))

f=(celsius*1.8)+32

c=(farenheit-32)/1.8

print("Temperature in celsius is:",c)

print("Temperature in farenheit is:",f)

o/p
Enter the temperature in celcius:37.5
Enter the temperature in farenheit:98
Temperature in celsius is: 36.666666666666664

Temperature in farenheit is: 99.5

Factorial:

def recur_factorial(n):

"""Function to return the factorial

of a number using recursion"""

if n == 1:

return n

else:

return n*recur_factorial(n-1)

# Change this value for a different result

num =int(input("Enter the number:"))

# uncomment to take input from the user


#num = int(input("Enter a number: "))

# check is the number is negative

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

print("The factorial of",num,"is",recur_factorial(num))

O/p:

Enter the number:5


The factorial of 5 is 120

You might also like