In this tutorial, we will learn how to write a script in C, C++, and Python to calculate the sum and average of three numbers. The sum of three numbers can be calculated by using the addition operator between the numbers and to find out their average we can divide the sum by 3.
Example 1
Input: 4 5 6 Output sum = 15 , average = 5
C Program to calculate sum and average of three numbers
#include <stdio.h>
int main()
{
int num1, num2, num3, sum=0;
float average;
//input 3 numbers from user
printf("Enter 3 Numbers eg(4 5 6): ");
scanf("%d %d %d", &num1, &num2, &num3);
//sum of three numbers
sum = num1+num2+num3;
//average of 3 numbers
average = sum/3;
printf("Sum of (%d %d %d) = %d \n", num1, num2, num3, sum);
printf("Average of (%d %d %d) = %.2f", num1, num2, num3, average);
return 0;
}
Output
Enter 3 Numbers eg(4 5 6): 11 15 16
Sum of (11 15 16) = 42
Average of (11 15 16) = 14.00
C++ Program to calculate sum and average of three numbers
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, sum=0;
float average;
//input 3 numbers from user
cout<<"Enter 3 Numbers eg(4 5 6): "; cin>>num1>>num2>>num3;
//sum of three numbers
sum = num1+num2+num3;
//average of 3 numbers
average = sum/3;
cout<<"Sum of ("<<num1 <<" "<<num2<<" "<<num3<<") ="<<sum<<endl;
cout<<"Average of ("<<num1 <<" "<<num2<<" "<<num3<<") ="<<average<<endl;
return 0;
}
Output
Enter 3 Numbers eg(4 5 6): 11 34 55
Sum of (11 34 55) =100
Average of (11 34 55) =33
Python Program
# input 3 numbers from user
num1, num2, num3 = map(int, input("Enter 3 Numbers eg(4 5 6): ").split())
# sum of three numbers
sum_ = num1+num2+num3;
# average of 3 numbers
average = sum_/3;
print(f"Sum of({num1} {num2} {num3}) = {sum_}")
print(f"Average of({num1} {num2} {num3}) = {average}")
Output
Enter 3 Numbers eg(4 5 6): 4 5 6
Sum of(4 5 6) = 15
Average of(4 5 6) = 5.0
Wrapping Up!
In this programming tutorial, we learned how to calculate the sum and average of three numbers in C, C++, and Python. If we analyze the above programs the time and space complexities both are O(1), which means a constant complexity .
If you like this article or have any suggestions please let us know by commenting down below.
Happy Coding!
People are also reading:
- Python Examples
- Python RegEx
- How many Centimeters in a Foot through Python?
- Rectangle & Pyramids Pattern in C++
- C Program to Design Love Calculator
- C Program to Convert Feet into Inches
- C Program to Extract a Portion of String
- WAP in C++ & Python & sort an Array Using Bubble Sort
- WAP to find the Sum of Series 1/2+4/5+7/8+…
- WAP to Find the Sum of Series 1^2+3^2+5^2+…..+(2n-1)^2
Leave a Comment on this Post