Membuat pola Bintang berbentuk Segitiga dengan C++

1. Segitiga Siku-siku







Source Code
#include <iostream>

using namespace std;

int main(){
cout << "Segitiga siku-siku \n\n";

for(int i = 1; i <= 5; i++){
for(int j = 1; j <= i; j++){
cout << "*" ;
}
cout << endl;
}
}

2. Segitiga Siku-siku Terbalik




Source code
#include <iostream>

using namespace std;

int main() {
int i,j;

for (i=1;i<=5;i++) {
for (j=i;j<=5;j++) {
cout << "*";
}
cout << endl;
}

    return 0;
}

3. Bagun datar Belah Ketupat





Source Code
#include <iostream>

using namespace std;

int main()
{
    int a, b, i, j, n, z;
    int val1, val2, val3, val4, val5;

    cout << "Masukkan nilai     : ";
    cin >> n;
    cout << endl;
    cout << "\t\t\t\t";

// Membentuk bagian atas
    for(i = n; i > 0; i--)
    {
        // Membentuk spasi kosong di depan angka
        val1 = i-1;
        for(j = 1; j <= val1; j++)
        {
            cout <<" ";
        }

        // Membentuk angka setelah spasi
        val2 = n-val1;
        for(a = 1; a <= val2; a++)
        {
            cout <<"*";
        }

        // Membentuk angka perulangan setelah batas tengah
        val3 = val2-1;
        for(b = val3; b >= 1; b--)
        {
            cout <<"*";//b
        }

        cout << endl;
        cout << "\t\t\t\t";
    }

// Membentuk bagian bawah
    z = n-1;
    for(i = z; i > 0; i--)
    {
        val4 = n-i;
        for(j = 1; j <= val4; j++)
        {
            cout <<" ";
        }

        for(a = 1; a <= i; a++)
        {
            cout <<"*"; //a
        }

        val5 = i-1;
        for(b = val5; b >= 1; b--)
        {
            cout <<"*"; //b
        }

        cout << endl;
        cout << "\t\t\t\t";
    }

        cout << endl;
        cout << endl;

    return 0;

}

4. Segitiga Sama Sisi


Source Code

#include <iostream>
using namespace std;
int main() {
cout << "Segitiga sama sisi \n\n";

 int i , j , k, l ;

 for(int i=5;i>=0;i--){
  for(int j=i;j>=0;j--){
   cout<<" ";
  }
  for(int k=1;k<=5-i;k++){
   cout<<"*";
  }
  for(int k=1;k<5-i;k++){
   cout<<"*";
  }
  cout<<endl;
}
}




Komentar

Postingan Populer