0% found this document useful (0 votes)
29 views16 pages

1.contoh Program 5.1: Percobaan 5

The document contains examples of C++ programs that demonstrate the use of basic programming concepts like loops (for, while, do-while), arrays (1D and 2D), and matrix operations (addition and multiplication). It includes sample code to calculate the volume and area of shapes, print patterns, add and multiply matrices, and store and retrieve values from arrays. The programs are intended as learning examples for basic programming constructs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views16 pages

1.contoh Program 5.1: Percobaan 5

The document contains examples of C++ programs that demonstrate the use of basic programming concepts like loops (for, while, do-while), arrays (1D and 2D), and matrix operations (addition and multiplication). It includes sample code to calculate the volume and area of shapes, print patterns, add and multiply matrices, and store and retrieve values from arrays. The programs are intended as learning examples for basic programming constructs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Percobaan 5

1.Contoh program 5.1


#include<iostream.h>

#include<conio.h>

void main()

clrscr();

float sisi,panjang,lebar,tinggi,jari,volume,luas,keliling;

int pil;

cout<<"========PILIHAN PERHITUNGAN========"<<endl;

cout<<"1. volume dan luas kubus"<<endl;

cout<<"2. volume dan luas balok"<<endl;

cout<<"3. Keliling dan luas lingkaran"<<endl;

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

cout<<" masukan pilihan [1,2,3] : ";

cin>>pil;

switch(pil)

case 1:

cout<<"======Menu Kubus======"<<endl;

cout<<"panjang sisi : ";

cin>>sisi;

volume=sisi*sisi*sisi;

luas=6*(sisi*sisi);

cout<<"volume : "<<volume<<endl;

cout<<"luas : "<<luas;

break;
case 2:

cout<<"======Menu Balok======"<<endl;

cout<<"panjang : ";

cin>>panjang;

cout<<"lebar : ";

cin>>lebar;

cout<<"tinggi : ";

cin>>tinggi;

volume=panjang*lebar*tinggi;

luas=2*((panjang*lebar)+(panjang*tinggi)+(lebar*tinggi));

cout<<"volume : "<<volume<<endl;

cout<<"luas : "<<luas<<endl;

break;

case 3:

cout<<"====Menu Lingkarang===="<<endl;

cout<<"jari-jari : ";cin>>jari; luas=3.14*jari*jari;

keliling=2*3.14*jari;

cout<<"luas : "<<luas<<endl;

cout<<"keliling : "<<keliling;

break;

default:cout<<"pilihan salah";

break;

getch();

}
2.Program 5.1

#include<iostream.h>

#include<conio.h>

void main()

int a;

for(a=0;a<5;a++)

cout<<"belajar for"<<endl;

getch();

3.Contoh program 5.2

#include <iostream.h>

#include <conio.h>

void main()

int a=0;

while(a<5)

cout<<"belajar while"<<endl; a++;

}
getch();

4.Contoh program 5.3

#include<conio.h>

#include <iostream.h>

int main()

int a, b;

for(a = 1; a <= 5; a++)

cout << "\n ";

for(b = a; b <= 5; b++)

cout << a << " ";

getch();

return 0;

5.Contoh Program 5.4

#include<conio.h>
#include <iostream.h>

int main()

int loop = 1;

do

cout << loop++ << " ";

while(loop <= 10);

getch();

return 0;

6.Contoh Program 5.5

#include <iostream.h>

#include <conio.h>

void main()

int a=1;

char ulangi;

do

cout<<a<<endl; a++;

cout<<"ulangi looping ?: ";

cin>>ulangi;

} while(ulangi=='y');
getch();

7.Contoh Program 6.1

#include <iostream.h>

#include <conio.h>

void main()

int a,nilai[6]; for(a=1;a<=5;a++)

{ cout<<"masukan nilai kedalam array ke-"<<a<<": ";

cin>>nilai[a];

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

cout<<"lihat array secara descending"<<endl;

for(int b=5;b>0;b--)

cout<<"isi dari array ke-"<<b<<" : "<<nilai[b]<<endl;

getch();

8.Contoh Program 6.2

#include <stdio.h>

#include <conio.h>

void main()
{

int two_dim[3][5] = {1, 2, 3, 4, 5,10, 20, 30, 40, 50,100, 200, 300, 400, 500};

int i, j;

for (i=0; i<3; i++)

for (j=0; j<5; j++) printf("%6d", two_dim[i][j]);

printf("\n");

}
getch();
}

9.Contoh Program 6.3

#include<iostream.h>

#include<conio.h>

main()

clrscr();

int A[3][2]={{5,3},{10,12},{4,5}};

for(int b=0;b<3;b++)

for(int c=0;c<2;c++)

cout<<"A["<<b<<"]["<<c<<"] : "<<A[b][c]<<endl;

}
getche();

return (0);

Latihan

1. program untuk mencetak deret 10 9 8 7 6 5 4 3 2 1


#include <iostream.h>
#include <conio.h>
void main(){
int a;
for(a=10; a>0; a--){
cout << " " << a;
}
getch();
}
2. Program mencetak segitiga bintang(menggunakan while / for)
#include <iostream.h>
#include <conio.h>
void main(){
int k,b;
for(k=1; k<5; k++){
for(b=k; b<5; b++){
cout << "* ";
}
cout << "\n" ;
}
getch();
}
3. Program mencetak 5 buah bilangan,dengan bilangan awal 8
#include <iostream.h>
#include <conio.h>
void main(){
int a, x=8,h;
for(a=1; a<6; a++){
h=x-a;
cout << "Bilangan ke " << a << " = " << h << " diperoleh dari ";
cout << x << " - " << a << endl ;
}
getch();
}

Percobaan 6

10. Program penjumlahan matrix

#include <conio.h>

#include <iostream.h>

void main()

int i,j,kola,kolb,bara,barb;

int data1[25][25],data2[25][25],hasil[25][25];

char jawab;

do

do

clrscr();

cout<<"Program Penjumlahan Matrix"<<endl;

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

cout<<endl;

cout<<"Input Matrix A "<<endl;

cout<<"Jumlah Baris Matrix A: "; cin>>bara;


cout<<"Jumlah Kolom Matrix A: "; cin>>kola;

cout<<endl;

cout<<"Input Matrix B"<<endl;

cout<<"Jumlah Baris Matrix B: "; cin>>barb;

cout<<"Jumlah Kolom Matrix B: "; cin>>kolb;

while ((kola!=kolb) || (bara!=barb));

cout<<endl;

for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

cout<<"Data A ["<<i<<","<<j<<"]: ";cin>>data1[i][j];

cout<<endl;

for(i=1; i<=barb; i++)

for(j=1; j<=kolb; j++)

cout<<"Data B ["<<i<<","<<j<<"]: ";cin>>data2[i][j];

}
for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

hasil[i][j] = data1[i][j] + data2[i][j];

cout<<endl;

clrscr();

cout<<"Hasil Penjumlahan Matrix A + Matrix B = Matrix C : "<<endl;

gotoxy(1,4);

cout<<"C = ";

for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

gotoxy(2+4*j,1+2*i);

cout<<data1[i][j];

gotoxy(15,4);

cout<<" + ";

for(i=1; i<=barb; i++)


{

for(j=1; j<=kolb; j++)

gotoxy(16+4*j,1+2*i);

cout<<data2[i][j];

gotoxy(30,4);

cout<<" = ";

for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

gotoxy(31+4*j,1+2*i);

cout<<hasil[i][j]<<" ";

cout<<endl;

getch();

cout<<endl;

cout<<"Mau Melakukan Perhitungan Lagi [Y/T]? : "; cin>>jawab;

while ((jawab == 'y') || (jawab == 'Y'));


}

11. Program Perkalian Matrix

#include <conio.h>

#include <iostream.h>

void main()

int i,j,kola,kolb,bara,barb;

int data1[25][25],data2[25][25],hasil[25][25];

char jawab;

do

do

clrscr();

cout<<"Program Perkalian Matrix"<<endl;

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

cout<<endl;

cout<<"Input Matrix A "<<endl;

cout<<"Jumlah Baris Matrix A: "; cin>>bara;

cout<<"Jumlah Kolom Matrix A: "; cin>>kola;

cout<<endl;

cout<<"Input Matrix B"<<endl;

cout<<"Jumlah Baris Matrix B: "; cin>>barb;


cout<<"Jumlah Kolom Matrix B: "; cin>>kolb;

while ((kola!=kolb) || (bara!=barb));

cout<<endl;

for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

cout<<"Data A ["<<i<<","<<j<<"]: ";cin>>data1[i][j];

cout<<endl;

for(i=1; i<=barb; i++)

for(j=1; j<=kolb; j++)

cout<<"Data B ["<<i<<","<<j<<"]: ";cin>>data2[i][j];

for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

{
hasil[i][j] = (data1[i][1] * data2[1][j]) + (data1[i][2] * data2[2][j]);

cout<<endl;

clrscr();

cout<<"Hasil Perkalian Matrix A x Matrix B = Matrix C : "<<endl;

gotoxy(1,4);

cout<<"C = ";

for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

gotoxy(2+4*j,1+2*i);

cout<<data1[i][j];

gotoxy(15,4);

cout<<" x ";

for(i=1; i<=barb; i++)

for(j=1; j<=kolb; j++)

gotoxy(16+4*j,1+2*i);
cout<<data2[i][j];

gotoxy(30,4);

cout<<" = ";

for(i=1; i<=bara; i++)

for(j=1; j<=kola; j++)

gotoxy(31+4*j,1+2*i);

cout<<hasil[i][j]<<" ";

cout<<endl;

getch();

cout<<endl;

cout<<"Mau Melakukan Perhitungan Lagi [Y/T]? : "; cin>>jawab;

while ((jawab == 'y') || (jawab == 'Y'));

You might also like