| Program Mencari Total Dari 1-n #include <iostream.h> |
| #include <conio.h> |
| class total |
| { |
| friend istream & operator >> ( istream &, total& ); |
| friend ostream & operator<< ( ostream &, total& ); |
| public: |
| total(); |
| void tambah(){jumlah = 0; |
| for (i = 1; i<=n; i++) |
| jumlah += i; |
| } |
| private: |
| int n,i,jumlah; |
| }; |
| total::total() |
| {cout<<"PROGRAM MENCARI TOTAL DARI 1 - N"<<endl; |
| }; |
| istream & operator >> ( istream & in, total & msuk ) |
| { |
| cout << "masukkan bilangan : " ; |
| in >> msuk.n; |
| return in; |
| } |
| ostream & operator<< ( ostream &out, total& keluar) |
| { |
| out<<"hasilnya totalnya = "<<keluar.jumlah<<endl; |
| return out; |
| } |
| main() |
| { |
| total hu; |
| cin >> hu; |
| hu.tambah(); |
| cout<<hu; |
| getch(); |
| return 0; |
| } |
Rabu, 23 Maret 2011
Program Menampilkan Angka Ganjil
| #include<iostream.h> |
| #include<conio.h> |
| class ganjil{ |
| private: |
| int i; |
| public: |
| void tampil_ganjil(int n){ |
| for(i=1;i<=n;i++) |
| if( i % 2 != 0) |
| cout << i << " "; |
| } |
| }; |
| int main(){ |
| int n; |
| cout <<"masukan nilai batas n : " ; |
| cin >> n; |
| ganjil x; |
| x.tampil_ganjil(n); |
| getch(); |
| return 0; |
| } |
Program Cetak Segitiga
| #include <iostream.h> |
| #include <conio.h> |
| #include <string.h> |
| class segitiga{ |
| private: |
| int tinggi,m,n; |
| public: |
| void cetak(){ |
| cout <<"Masukan tinggi : "; |
| cin >> tinggi; |
| for(m=1;m<=tinggi;m++) |
| { |
| for(n=1;n<=m;n++) |
| cout << " * "; |
| cout << endl; |
| } |
| } |
| }; |
| int main(){ |
| cout << " *** Menggambar segitiga ***\n\n"; |
| segitiga x; |
| x.cetak(); |
| getch(); |
| return 0; |
| } |
Langganan:
Komentar (Atom)