Sunday 3 November 2024

Contoh Program Perulangan

 #include <iostream>

using namespace std;

int main()

{

int pilihan;

cout<<" Jenis Pilihan 1: 2: 3: 4:"<<endl;

atas:

cout<<" Menu Pilihan : ";cin>>pilihan;

if(pilihan==1)

{

  cout << "##  Program C++ Persegi Bintang  ##" << endl;

  cout << "===================================" << endl;

  cout << endl;

  int besar_persegi,i,j;

  cout << "Input besar persegi: ";

  cin >> besar_persegi;

 

  cout << endl;

 

  for(i=1;i<=besar_persegi;i++) {

    for(j=1;j<=besar_persegi;j++) {

      cout << " *";

    }

    cout << endl;

  }

}

else if(pilihan==2)


{

  cout << "##  Program C++ Segitiga Bintang  ##" << endl;

  cout << "====================================" << endl;

  cout << endl;

 

  int tinggi_segitiga,i,j;

 

  cout << "Input tinggi segitiga: ";

  cin >> tinggi_segitiga;

 

  cout << endl;

 

  for(i=1;i<=tinggi_segitiga;i++) {

    for(j=1;j<=i;j++) {

      cout << " *";

    }

    cout << endl;

  }

}

 

 else if(pilihan==3)

 

 {

  cout << "##  Program C++ Segitiga Bintang Terbalik  ##" << endl;

  cout << "=============================================" << endl;

  cout << endl;

 

  int tinggi_segitiga,i,j;

 

  cout << "Input tinggi segitiga: ";

  cin >> tinggi_segitiga;

 

  cout << endl;

 

  for(i = 0; i < tinggi_segitiga; i++) {

    for(j = 1; j <= tinggi_segitiga-i; j++) {

      cout << " *";

    }

    cout << endl;

  }

 }

 

 else if(pilihan==4)

 {

 

  cout << "##  Program C++ Belah Ketupat Bintang  ##" << endl;

  cout << "=========================================" << endl;

  cout << endl;

 

  int lebar_belah_ketupat,i,j,k;

 

  cout << "Input lebar belah ketupat: ";

  cin >> lebar_belah_ketupat;

 

  cout << endl;

 

  for(i=1;i<=lebar_belah_ketupat;i++) {

 

    for(j=1;j<=lebar_belah_ketupat-i;j++) {

      cout << " ";

    }

 

    for(k=1;k<=i;k++) {

      cout << " *";

    }

    cout << endl;

  }

 

  for(i=1;i<lebar_belah_ketupat;i++) {

 

    for(j=1;j<=i;j++) {

      cout << " ";

    }

 

    for(k=1;k<=lebar_belah_ketupat-i;k++) {

      cout << " *";

    }

    cout << endl;

  }

 }

 

 goto atas;

  return 0;

}

No comments:

Post a Comment