MENCARI RERATA

kali ini saya akan membagikan program mencari nilai rata-rata dari suatu bilangan.
runing progam 

C++

#include <iostream>
#include <string>

using namespace std;

class rata_rata{ private : int n;
                      int h ;
                      int a;
                      float hasil;
                     
            public : rata_rata();
                     void input();
                     float rerata(float n, float h, float a);     
                     void output();
     
      };
     
      rata_rata::rata_rata(){cout<<"RERATA"<<endl;}
     
      void rata_rata::input(){
                          h = 0; cout << "masukkan nilai n = ";cin >> n;
                          a = n;
                         }

       float rata_rata::rerata (float n, float h, float a)
                { if (n==0) return hasil = h / a;
                  else { h = h + n;
                        return  rerata(n - 1,h,a);
                        }
                 }

       void rata_rata::output(){cout<<"Rata-rata = "<<rerata (n,h,a)<<endl;} 
                    
int main(int argc, char *argv[])

{
   rata_rata x;
    x.input();
    x.output();
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

No comments:

Berkomentarlah sesuai artikel yang kamu baca.

Powered by Blogger.