➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
Write a C++ Program to Find Average of 3 Numbers Using Function. In this program, we will take 3 numbers as input from the end-user, and pass them to a function to calculate the average of them. After calculating the average value that function will return a floating-point value, which will be stored to a variable and displayed on the screen.
The entered number can be integer type or floating-point type therefore it is better to use float or double data type to store the input values.
Find Average of 3 Numbers Using Function in C++
C++ Program to Find Average of 3 Numbers Using Function
#include<iostream>
using namespace std;
/* function to calculate average of
* three numbers.
*/
double average(double n1, double n2, double n3)
{
return (n1 + n2 + n3)/3;
}
// main function
int main()
{
double a, b, c; // to store numbers
double avg; // to store result value
// take three numbers as input values
cout << "Enter three numbers: ";
cin >> a >> b >> c;
// function to find avg of 3 numbers
avg = average(a, b, c);
// display result
cout << "Average = " << avg << endl;
return 0;
}
Output for the different input values:-
Enter three numbers: 10 20 30
Average = 20
Enter three numbers: 15 26 30
Average = 23.6667
Enter three numbers: 15.5 20.6 19.32
Average = 18.4733
The average() function takes three numbers as floating-point values, and calculates its average value as (n1 + n2 + n3)/3. Then it returns the average value back to the caller function.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Learn More C++ Programming Examples,
- Introduction to C++ Programming
- Data Types in C++ Programming
- Find Range of Data Types in C++
- Void main(), main() vs int main()
- Hello World! Program in C++
- Addition of Two Numbers in C++
- Add Subtract Multiply Divide 2 Numbers
- Sum and Average of three numbers
- Area of Circle Triangle Rectangle
- Calculate Simple Interest in C++
- C++ program to find ASCII value
- C++ program to swap two Number
- Check Even or Odd Number in C++
- Find Positive Negative Zero in C++
- Check Vowel or Consonant in C++
- Greatest of Three Numbers in C++
- Program to Check Leap Year in C++
- Simple Calculator Program in C++
- Program to Reverse a Number in C++
- Sum of Natural Numbers in C++
- C++ Program for GCD of Two Numbers
- C++ Program for LCM of Two Numbers
- Find Power of a Number in C++
- Fibonacci Series Program in C++
- Check Palindrome Number in C++
- Factorial of a Number Program in C++
- Factorial Using Recursion in C++
- Prime Number Program in C++
- Prime Number b/w 1 to N in C++