Assamualaikum teman-teman..
Program ini dibuat menggunakan perulangan for dan percabgan switch. Untuk membuat bentuk pola segitiga digunakan perulangan for yang objeknya simbol bintang dan spasi. Diprogram ini terdapat 6 pola segitiga dan 1 layang-layang. Berikut adalah gambar ilustrasinya.
Gambar ilustrasi :
Source code
#include <iostream> using namespace std; int main(){ int n,p; cout<<"================================"<<endl; cout<<"| |"<<endl; cout<<"| Program Pola Segitiga |"<<endl; cout<<"| |"<<endl; cout<<"================================"<<endl; cout<<"| + Pola 1 |"<<endl; cout<<"| + Pola 2 |"<<endl; cout<<"| + Pola 3 |"<<endl; cout<<"| + Pola 4 |"<<endl; cout<<"| + Pola 5 |"<<endl; cout<<"| + Pola 6 |"<<endl; cout<<"| + Pola 7 |"<<endl; cout<<"--------------------------------"<<endl; cout<<"| Pilih Pola : ";cin>>p; switch(p){ case 1: cout<<"| Masukan panjang pola : ";cin>>n; cout<<"--------------------------------"<<endl; for (int i=1; i<=n; i++){ for(int j=1; j<=i; j++){ cout<<"*"; } cout<<endl; } cout<<"--------------------------------"<<endl; break; case 2: cout<<"| Masukan panjang pola : ";cin>>n; cout<<"--------------------------------"<<endl; for (int i=1; i<=n; i++){ for(int j=n; j>=i; j--){ cout<<"*"; } cout<<endl; } cout<<"--------------------------------"<<endl; break; case 3: cout<<"| Masukan panjang pola : ";cin>>n; cout<<"--------------------------------"<<endl; for (int i=1; i<=n; i++){ for(int j=1; j<i; j++){ cout<<" "; } for(int k=n; k>=i; k--){ cout<<"*"; } cout<<endl; } cout<<"--------------------------------"<<endl; break; case 4: cout<<"| Masukan panjang pola : ";cin>>n; cout<<"--------------------------------"<<endl; for (int i=1; i<=n; i++){ for(int j=n; j>i; j--){ cout<<" "; } for(int k=1; k<=i; k++){ cout<<"*"; } cout<<endl; } break; case 5: cout<<"| Masukan panjang pola : ";cin>>n; cout<<"--------------------------------"<<endl; for (int i=1; i<=n; i++){ for(int j=n; j>i; j--){ cout<<" "; } for(int k=1; k<=(2*i-1); k++){ cout<<"*"; } cout<<endl; } break; case 6: cout<<"| Masukan panjang pola : ";cin>>n; cout<<"--------------------------------"<<endl; for (int i=1; i<=n; i++){ for(int j=1; j<i; j++){ cout<<" "; } for(int k=n; k>=(2*i-n); k--){ cout<<"*"; } cout<<endl; } break; case 7: cout<<"| Masukan panjang pola : ";cin>>n; cout<<"--------------------------------"<<endl; for (int i=1; i<=n; i++){ for(int j=n; j>i; j--){ cout<<" "; } for(int k=1; k<=(2*i-1); k++){ cout<<"*"; } cout<<endl; } for (int i=2; i<=n; i++){ for(int j=1; j<i; j++){ cout<<" "; } for(int k=n; k>=(2*i-n); k--){ cout<<"*"; } cout<<endl; } break; default : cout<<"================================"<<endl; cout<<"| MAAF, Data diluar jangkauan |"<<endl; cout<<"================================"<<endl; } cin.get(); return 0; }
Running program
Tampilan segitiga pola 1Tampilan segitiga pola 2
Tampilan segitiga pola 3
Tampilan segitiga pola 4
Tampilan segitiga pola 5
Tampilan segitiga pola 6
Tampilan Layang-layang pola 7