VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-find-the-average-of-two-numbers/

⇱ Program to find the average of two numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find the average of two numbers

Last Updated : 23 Jul, 2025

Given two Integers A and B, your task is to find the average of two numbers where the average is given by the formula:
average(A, B) = (A+B)/2

Examples:

Input: A=5, B=3
Output: 4
Explanation: Average(A,B) = (A+B)/2 = 8/2 =4

Input: A=10, B=5
Output: 7.5
Explanation: Average(10, 5) = (10 + 5)/2 = 15/2 = 7.5

Approach: To solve the problem, follow the below idea:

We can find the average of two number by summing the number using the '+' operator and then divide the sum by 2 to get the final answer.

Step-by-step algorithm:

  • Read the integers A and B from the user.
  • Calculate the sum: C = A + B
  • Calculate the average: average = sum / 2
  • Display the result.

Below is the implementation of the algorithm:


Output
Average of 10 and 5 = 7.5

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: