➤ 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
In this post, we will write a C++ program to find the Sum and Average of three numbers. First, we develop a simple program, then we will C++ program to find the Sum and Average of three numbers using the function concept.
Let us first develop a Simple C++ program to find the sum and average of three numbers.
#include<iostream>
using namespace std;
int main()
{
// declare variables
double num1, num2, num3;
double sum, average;
// take input from end-user
cout << "Enter three Numbers :: ";
cin >> num1 >> num2 >> num3;
// calculate sum value
sum = num1 + num2 + num3;
// calculate average value
average = sum / 3;
// display result
cout << "Sum = " << sum << endl;
cout << "Average = " << average << endl;
return 0;
}
Output:-
Enter three Numbers: 10 15 20
Sum = 45 Average = 15
In this C++ program, we define three variables num1, num2, and num3 of float data types. These variables store data given by the user. Two other variables sum and average are defined to store sum and average value after calculation.
The sum is calculated by adding variables. sum = num1 + num2 + num3. The average value is calculated by the sum of variables divided by the number of variables. Here, the number of variables is three. So, average = sum / 3 calculates the average value.
C++ Program to Find Sum and Average of Three Numbers Using the Function
A function is a block of code that performs a specific task. For example, the main is function and every program execution starts from the main function in C++ programming.
C++ Program to Find Sum and Average of Three Numbers Using the Function
#include<iostream>
using namespace std;
// declare functions
double add(double num1, double num2, double num3);
double avg(double sum, int n);
// main function
int main()
{
// declare variables
double num1, num2, num3;
double sum, average;
// take input from end-user
cout << "Enter three Numbers :: ";
cin >> num1 >> num2 >> num3;
// calculate sum value
sum = add(num1 , num2, num3);
// calculate average value
average = avg(sum, 3);
// display result
cout << "Sum = " << sum << endl;
cout << "Average = " << average << endl;
return 0;
}
// function to add three numbers
double add(double num1, double num2, double num3)
{
return num1+num2+num3;
}
// function to calculate average of n number
double avg(double sum, int n)
{
return sum / n;
}
Output:-
Enter three Numbers :: 20 30 45
Sum = 95
Average = 31.6667
Also Read:- Database Management System Tutorial
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!
Follow Us
Instagram • Twitter • Youtube • Linkedin • Facebook • Pinterest • Telegram
Learn More C++ Programming Examples,
- Hello World Program in C++
- C++ program to find Addition of Two Numbers
- Add, subtract, divide & multiply two numbers in C++
- Find the area of Circle, Triangle and, Rectangle in C++
- C++ program to calculate the Simple Interest
- 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++
- C++ Program for GCD of Two Numbers
- C++ Program for LCM of Two Numbers