# C++ | Average of numbers

Introduction

This is program to calculate the average of numbers. Like Ones before First program will directly calculate the average of two declared numbers which is non-flexible program. Second program will let you add numbers from keyboard by cin command and program will calculate the average of them. The later programs are modified to add more numbers to take out the average of more numbers.At the same time after learning the program you can extend the program.Go enjoy the program. Lets begin……

First Program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b;
//declare variable avg as float (ans in point).
float avg;
//declare the two numbers save them in 'a' and 'b'
a=2;
b=4;
//calculate average and save them in 'avg'
avg=(a+b)/2;
//show the output average
cout<<"Average is equal to "<<avg;
//get character
getch();
}
``````

output

Average is equal to 3

Second Program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b;
//declare variable avg as float (ans in point).
float avg;
//Input the two numbers save them in 'a' and 'b'
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
//calculate average and save them in 'avg'
avg=(a+b)/2;
//show the output average
cout<<"Average is equal to "<<avg;
//get character
getch();
}
``````

Output

Enter the first no.
3
Enter the second no.
6
Average is equal to 4.5

Third Program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c;
//declare variable avg as float (ans in point).
float avg;
//Input the two numbers save them in 'a' and 'b' and 'c'
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
cout<<"Enter the third no."<<endl;
cin>>c;
//calculate average and save them in 'avg'
avg=(a+b+c)/3;
//show the output average
cout<<"Average is equal to "<<avg;
//get character
getch();
}
``````

Output

Enter the first no.
3
Enter the second no.
6
Enter the third no.
3
Average is equal to 4

Fourth Program

``````
#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a,b,c,d;
//declare variable avg as float (ans in point).
float avg;
//Input the two numbers save them in 'a' , 'b' , 'c','d'
cout<<"Enter the first no."<<endl;
cin>>a;
cout<<"Enter the second no."<<endl;
cin>>b;
cout<<"Enter the third no."<<endl;
cin>>c;
cout<<"Enter the fourth no."<<endl;
cin>>d;
//calculate average and save them in 'avg'
avg=(a+b+c+d)/4;
//show the output average
cout<<"Average is equal to "<<avg;
//get character
getch();
}
``````

Output

Enter the first no.
3
Enter the second no.
6
Enter the third no.
3
Enter the fourth no.
9
Average is equal to 5.25

How does it work

1. You enter the numbers from keyboard and are save in respective variables.
2. The average is calculated and save in avg variable.
3. The average is displayed on screen.

Extending it

You can extend the program by adding more variables and update the avg formula based on number of variables taken except the avg variable.

Explanation.

1. Include ‘iostream.h’ and ‘conio.h’ files.
3. Start program by first clearing the screen.
4. Declare the variables as int and avg as float (name them as you want.)
5. Add cout and cin of variables you want to take average of.(as shown in program)
6. Calculate the average of  the variables and save them in avg variable.
7. Cout the avg variable.

At the end

You learnt creating the c++ program of average of numbers. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: