All Python Programs

All Programs

Basic Python Programs

Simple Python programs

If Condition Programs

If condition based Python Programs 

Looping based programs

program based on for loop, while loop, range

Searching and Sorting

Python Searching Programs
Python Sorting Programs

Python program to print sum, product, difference, and the average of two numbers

Solution: with out using functions
Solution: using functions without passing arguments
Solution: using functions and by passing arguments
Solution: using multiple functions without passing arguments
Solution: using multiple functions and by passing arguments
Solution: using multiple functions and by passing arguments and returning values


Solution : Without using function
Program/Source Code

a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
s=a+b
p=a*b
if(a>b):
    d=a-b
else:
    d=b-a
av=(a+b)/2
print("Sum = ",s)
print("prod = ",p)
print("diff = ",d)
print("avg = ",av)

Output:

Enter 1st no 2
Enter 2nd no 5
Sum = 7
prod = 10
diff = 3
avg = 3.5
>>>



Solution: using functions without passing arguments
Program/Source Code

#function definition

def calculate():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    s=a+b
    p=a*b
    if(a>b):
        d=a-b
    else:
        d=b-a
    av=(a+b)/2
    print("Sum = ",s)
    print("prod = ",p)
    print("diff = ",d)
    print("avg = ",av)

#function calling
calculate()

Output:

Enter 1st no 25
Enter 2nd no 6
Sum = 31
prod = 150
diff = 19
avg = 15.5
>>>



Solution: using functions and by passing arguments
Program/Source Code

#function definition

def calculate(a,b):
    s=a+b
    p=a*b
    if(a>b):
        d=a-b
    else:
        d=b-a
    av=(a+b)/2
    print("Sum = ",s)
    print("prod = ",p)
    print("diff = ",d)
    print("avg = ",av)

#function calling
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
calculate(a,b)

Output:

Enter 1st no 2
Enter 2nd no 3
Sum = 5
prod = 6
diff = 1
avg = 2.5
>>>



Solution: using multiple functions without passing arguments
Program/Source Code

#function definition

def sum1():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    s=a+b
    print("Sum = ",s)
def prod():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    p=a*b
    print("prod = ",p)
def diff():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    if(a>b):
        d=a-b
    else:
        d=b-a
    print("diff = ",d)
def avg():
    a=int(input("Enter 1st no "))
    b=int(input("Enter 2nd no "))
    av=(a+b)/2
    print("avg = ",av)

#function calling
sum1()
prod()
diff()
avg()

Output:

Enter 1st no 10
Enter 2nd no 20
Sum = 30
Enter 1st no 2
Enter 2nd no 3
prod = 6
Enter 1st no 2
Enter 2nd no 6
diff = 4
Enter 1st no 5
Enter 2nd no 3
avg = 4.0
>>>



Solution: using multiple functions and by passing arguments
Program/Source Code

#function definition

def sum1(a,b):
    s=a+b
    print("Sum = ",s)
def prod(a,b):
    p=a*b
    print("prod = ",p)
def diff(a,b):
    if(a>b):
        d=a-b
    else:
        d=b-a
    print("diff = ",d)
def avg(a,b):
    av=(a+b)/2
    print("avg = ",av)

#function calling
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
sum1(a,b)
prod(a,b)
diff(a,b)
avg(a,b)

output:

Enter 1st no 6
Enter 2nd no 2
Sum = 8
prod = 12
diff = 4
avg = 4.0
>>>



Solution: using multiple functions and by passing arguments and returning values
Program/Source Code

#function definition

def calculate(a,b):
    s=a+b
    p=a*b
    if(a>b):
        d=a-b
    else:
        d=b-a
    av=(a+b)/2
    return s,p,d,av
    

#function calling
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
s,p,d,av=calculate(a,b)
print("Sum = ",s)
print("prod = ",p)
print("diff = ",d)
print("avg = ",av)

Output:

Enter 1st no 10
Enter 2nd no 20
Sum = 30
prod = 200
diff = 10
avg = 15.0
>>>

Previous :Python program to print square and cube of a number

Next :Python program to calculate and print area and perimeter of square

You May Also Like:

Python program to print sum of two numbers.
Python Program to Find Factorial of Number Using Loop
C Language Program to Print Fibonacci Series
Python Program for Armstrong Number
Python Program to convert lower case alphabet to upper case alphabet
C Language Program for Prime Number

Tutorials

Technical Questions

Interview Questions

C Programming
C++ Programming
Basic Python Tutorial
Advanced Python Tutorial
C Language
C++ Programming
Python programming

C Interview Questions
C++ Interview Questions
Python Interview Questions
HTML Interview Questions