Program of Average of two Numbers in Python
Program of Average of two Numbers in Python
Average means arithmetic mean which can be calculated by the following formula
Average = Sum of all numbers/ Total numbers
for example: if we have to calculate the average of 3 and 7, so average of these numbers can be calculated as follows:
average = (3 + 7)/2
average = 5.0
Since the total numbers are two so we divided the sum of all the numbers by 2.
for example: if we have to calculate the average of 3, 6 and 9, so average of these numbers can be calculated as follows:
average = (3+6+9)/3
average = 6.0
Since the total numbers are three so we divided the sum of all the numbers by 3.
Suppose we have to calculate the average of 5, 10, 20, 15 and 10, so average of these numbers can be calculated as follows:
average = (5 + 10 + 20 + 15 + 10)/5
average = 12.0
Since the total numbers are five so we divided the sum of all the numbers by five.
Let we do the python program
Write a program in python to calculate the average of two numbers (integers).
num1 = int(input("Enter first number : ")) #Accepting first integer from user num2 = int(input("Enter second number : "))#Accepting second integer from user avrg = (num1 + num2)/2 #Calculating average print("Average of two numbers is : ", avrg)#Displaying the average
Line by Line explanation of above code
- First line of code is for accepting a number(integer) from the user.
- Second line of code is for accepting another number(integer) from the user.
- In third line, we are calculating the average by adding both numbers and divided by 2. Result of average is stored in a variable “avrg” (You can take any other variable name)
- Fourth Line is simply printing the result.
OUTPUT
Similarly we can also calculate the average of two floating point numbers
num1 = float(input("Enter first number : ")) #Accepting first from user num2 = float(input("Enter second number : "))#Accepting second from user avrg = (num1 + num2)/2 #Calculating average print("Average of two numbers is : ", avrg)#Displaying the average
OUTPUT
NOTE: We can accept either integer or floating numbers from user
Write a program in python to calculate the average of three numbers.
num1 = int(input("Enter first number : ")) #Accepting first integer from user num2 = int(input("Enter second number : "))#Accepting second integer from user num3 = int(input("Enter third number : ")) avrg = (num1 + num2 + num3)/3 #Calculating average print("Average of three numbers is : ", avrg)#Displaying the average
OUTPUT
Average of two numbers in Python
MCQ of Computer Science Chapter Wise
2. Flow of Control (Loop and Conditional statement)
3. 140+ MCQ on Introduction to Python
4. 120 MCQ on String in Python
7. 100+ MCQ on Flow of Control in Python
8. 60+ MCQ on Dictionary in Python
Important Links
100 Practice Questions on Python Fundamentals
120+ MySQL Practice Questions
90+ Practice Questions on List
50+ Output based Practice Questions
100 Practice Questions on String
70 Practice Questions on Loops
70 Practice Questions on if-else
Disclaimer : I tried to give you the correct coding of ” Average of two numbers in Python” , but if you feel that there is/are mistakes in the code or explanation of “Average of two numbers in Python “ given above, you can directly contact me at csiplearninghub@gmail.com.
Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post
I just like the helpful information you provide in your articles
I like the efforts you have put in this, regards for all the great content.
Windows 11 Pro cheapest digital license key