BCS-1A

Friday, January 13, 2012

Calculate AM,GM and HM(for loop)


/***********************************************************
  Name: Calculate AM,GM and HM
 
  Copyright: BCS-1A
 
  Author: Muhammad Hussnain Imtiaz
 
  Date: 27/12/11 16:29
 
  Description: Program To calculat Arithmatic Mean,
                 Geomatric Mean and Harmonic Mean
***********************************************************/
// preprocesser directive
 #include<iostream>
 #include<conio.h>
 #include<cmath>
 using namespace std;
 int main()
 {
     // Local declaration
     double number       =0.0,
            sum          =0.0,
            product      =1.0,
            sumReciprocal=0.0,
            AM=0.0,
            GM=0.0,
            HM=0.0;
     int i=0,
         n=0,
         code;

     cout << "  Program To calculat Arithmatic Mean, Geomatric Mean and Harmonic Mean"
             <<endl<<endl ;
    for (i=1; code != 12345 ; i++)  // 1st for loop, just for lock
{
            cout << "\n                       Enter code :";
            cin  >> code ;
            system ("CLS");
            if (code == 12345) // 1st if loop for lock
               {
     cout << "                How many numbers you want to enter :" ;
     cin  >> n;
     cout<<endl;
     for (i=1; i<=n; i++)
     {
         cout << "                    Enter number " << i << ":";
         cin  >> number;
         sum = sum+number;
         product=product*number;
         sumReciprocal=sumReciprocal+(1.0/number);
     }// end of for loop
 // Counting AM,GM and HM
     AM=sum/n;
     GM=pow(product,1.0/n);
     HM=n/sumReciprocal;
         cout <<endl
              <<endl
              <<endl
              << "                    Arithmatic Mean :" << AM << endl;
         cout << "                    Geomatric  Mean :" << GM << endl;
         cout << "                    Harmonic   Mean :" << HM << endl;
         cout <<endl
              <<endl
              << "                     Press any key" ;
     getch();
               } // end of if
            else
               {
                cout << "          "
                     << "you enter invalid code, press 'Enter' to retry." ;
                getch();
               }
 } // end of for loop (Lock)
   
     system ("CLS");
     cout <<endl
          <<endl
          <<endl
          <<endl
          <<endl
          << "                 Created By:Mohammad Hussnain Imtiaz" ;
            getch ();
            return 0;
 }// end of main function

No comments:

Post a Comment