Pages

Monday, July 08, 2013

Write a program that calculate Average of Four Numbers in C++

Calculate average of four numbers and print the numbers and their deviation from the Average

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   #include <iostream.h>
   #include <iomanip.h>
   #include <conio.h>
   
   int main ()
   {  
     cout << "\nEnter the First number  : ";
     int num1;
     cin  >> num1 ;
     cout << "\nEnter the Second number : ";
     int num2;
     cin  >> num2 ;
     cout << "\nEnter the Third number  : ";
     int num3;
     cin  >> num3 ;
     cout << "\nEnter the Fourth number : ";
     int num4;
     cin  >> num4 ;
            int sum =  num1 + num2 + num3 + num4;
      float average = sum /4.0 ;
     cout << fixed ;
     cout << showpoint;
     cout << setprecision (2);
     cout << "\n ******** Average is " << average << " ******** \n";
     cout << "\n First  no : " << num1 << " -- Deviation : " << setw(8) << (num1 - average) ;  
     cout << "\n Second no : " << num2 << " -- Deviation : " << setw(8) << (num2 - average) ;
     cout << "\n Third  no : " << num3 << " -- Deviation : " << setw(8) << (num3 - average) ;
     cout << "\n Fourth no : " << num4 << " -- Deviation : " << setw(8) << (num4 - average) ;
       getch();
       return 0;
              }//main

Output:

Enter the first number  : 24

Enter the second number : 56
 
Enter the third number  : 46

Enter the fourth number : 24

******** Average is 37.50 ********

 First  no : 24 -- Deviation :        -13.50
 Second no : 56 -- Deviation :         18.50
 Third  no : 46 -- Deviation :          8.50 
 Fourth no : 24 -- Deviation :        -13.50

No comments:

Post a Comment