Deret aritmatika c++

#include <cstdlib>

#include <iostream>

 

using namespace std;

class aritmatika{

friend istream& operator>>(istream&, aritmatika&);

friend ostream& operator<<(ostream&, aritmatika&);

public:

aritmatika();

void cetak();

void suku();

void jumlah();

private:

int x[100];

int a,b,n,un,sn;

};

istream& operator>>(istream& mlebu, aritmatika& s){

cout<<"Masukkan Suku Pertama :"; mlebu>>s.a; cout<<endl;

cout<<"Masukkan Beda :"; mlebu>>s.b; cout<<endl;

cout<<"Masukkan Jumlah Suku :"; mlebu>>s.n; cout<<endl;

return mlebu;

}



ostream& operator<<(ostream& metu, aritmatika& v){

metu<<"Suku Pertama :"<<v.a<<endl;

metu<<"Beda :"<<v.b<<endl;

metu<<"Jumlah Suku :"<<v.n<<endl;

metu<<"Deret Aritmatika :";

for(int i=0; i<v.n; i++){

metu<<v.x[i]<<",";

}

metu<<endl;

metu<<"Jumlah Suku Ke-n :"<<v.un<<endl;

metu<<"Jumlah Semua Suku :"<<v.sn<<endl;

return metu;}

aritmatika::aritmatika(){

cout<<"\t\t<< PROGRAM DERET ARITMATIKA

>>"<<endl;

}

void aritmatika::cetak(){

x[0]=a;

x[1]=a+b;

for(int i=2; i<n; i++){

x[i]=a+(b*i);

}

}

void aritmatika::suku(){

int t;

cout<<"Masukkan Suku Yang Ingin Di cari :"; cin>>t;

un=a+(t-1)*b;

}

void aritmatika::jumlah(){

sn=(n/2)*(2*a+(n-1)*b);

}

int main(int argc, char *argv[])

{

aritmatika arif;

cin>>arif;

arif.cetak();

arif.suku();

arif.jumlah();

cout<<arif;

system("PAUSE");

return EXIT_SUCCESS;

}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s