Contoh PROGRAM PERULANGAN C++
Koleksi Program Perulangan C++ |
SUBSTANSI :
- for
- do .. while
- while
- Kombinasi for dan do .. while
- Membuat Bidang ( Perulangan dan Percabangan - > switch)
1. Perulangan Menggunakan "for"
Mencari Rata-Rata, Maksimum dan Minimum |
Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//MENCARI RATA - RATA, MAKSIMUM DAN MINIMUM
void main() { randomize(); int data, mak, min,tot=0; float rata; for(int i=1;i<=5;i++) { data=random(100); cout<<"Data masuk : "<<data<<endl; if (i==1) mak=min=data; else{ if (mak<data) mak=data; if (min>data) min=data; } tot=tot+data; } rata=tot/5; cout<<"Rata-rata : "<<rata<<endl; cout<<"Masimum : "<<mak<<endl; cout<<"Minimum : "<<min<<endl; getch(); }
Counter 1st Run |
Counter 2nd Run |
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//COUNTER
void main() { for(int i=1;i<=1000;i++) { cout<<setw(5)<<i; if(i%10==0) cout<<endl; if(i%100==0) { cout<<"Tekan enter untuk melanjutkan..."; getch(); clrscr(); } } getch(); }
Counter dengan Interval, 1st Run |
Counter dengan Interval, 2st Run |
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//COUNTER DENGAN INTERVAL
void main() { int brs=0,klm=0; for(int i=1;i<=1000;i+=3) { cout<<setw(5)<<i; klm++; if(klm==10) { cout<<endl; brs++; klm=0; } if(brs==10) { cout<<"Tekan enter untuk melanjutkan..."; getch();clrscr(); brs=0; } } getch(); }
2. Perulangan Menggunakan do .. while
Mencari Rata-Rata, Maksimum dan Minimum |
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//MENCARI RATA-RATA, MAKSIMUM DAN MINIMUM
void main() { randomize(); int data, mak, min,tot=0,i=1; float rata; do{ data=random(100); cout<<"Data masuk : "<<data<<endl; if (i==1) mak=min=data; else{ if (mak<data) mak=data; if (min>data) min=data; } tot=tot+data; i++; }while(i<=5); rata=tot/5; cout<<"Rata-rata : "<<rata<<endl; cout<<"Masimum : "<<mak<<endl; cout<<"Minimum : "<<min<<endl; getch(); }
3. Perulangan Menggunakan while
Mencari Rata-Rata, Maksimum dan Minimum |
Program :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
//MENCARI RATA-RATA, MAKSIMUM DAN MINIMUM
void main()
{
randomize();
int data, mak, min,tot=0,i=1;
float rata;
while(i<=5)
{
data=random(100);
cout<<"Data masuk : "<<data<<endl;
if (i==1)
mak=min=data;
else
{
if (mak<data)
mak=data;
if (min>data)
min=data;
}
tot=tot+data;
i++;
}
rata=tot/5;
cout<<"Rata-rata : "<<rata<<endl;
cout<<"Masimum : "<<mak<<endl;
cout<<"Minimum : "<<min<<endl;
getch();
}
4. Kombinasi for dan do .. while
Nota Pembayaran |
Program :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//NOTA PEMBAYARAN
void main()
{
cout<<"NOTA PENJUALAN\n";
cout<<"GATEWAN MARKET\n";
cout<<"Jl. Parangtritis No 10 Yogyakarta\n";
cout<<"===================================================================\n";
cout<<"| No. | Nama Barang | Jml | Hg Sat | Jml Hg | Diskon | Tot Hg |\n";
cout<<"===================================================================\n";
for(int i=1;i<=15;i++)
{
cout<<"| |\n";
}
cout<<"===================================================================\n";
cout<<"| TOTAL Bayar \n";
cout<<"===================================================================\n";
int i=1; char jw;
float jml,hgsat,jmlhg,disk,hgdisk,totdisk,totsemua,tothg;
char nmbrg[30];
do
{
gotoxy(3,6+i);cout<<i;
gotoxy(8,6+i);cin>>nmbrg;
gotoxy(22,6+i);cin>>jml;
gotoxy(28,6+i);cin>>hgsat;
jmlhg=jml*hgsat;
gotoxy(37,6+i);cout<<jmlhg;
gotoxy(46,6+i);cin>>disk;
hgdisk=(disk/100)*jmlhg;
gotoxy(50,6+i);cout<<hgdisk;
tothg=jmlhg-hgdisk;
totdisk=totdisk+hgdisk;
totsemua=totsemua+tothg;
gotoxy(55,6+i);cout<<tothg;
gotoxy(50,23);cout<<totdisk;
gotoxy(55,23);cout<<totsemua;
gotoxy(30,1);cout<<"Input data lagi [y/t]? ";cin>>jw;
i++;
gotoxy(30,1);clreol();
}while(jw=='y');
getch();
}
5. Membuat Bidang ( Perulangan dan Percabangan - > switch)
Diagonal Bawah |
Diagonal Atas |
Silang |
Segitiga Siku-Siku Terbalik Kiri |
Segitiga Siku-Siku Tegak |
Segitiga Siku-Siku Terbalik Kanan |
Program :
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
//Membuat Bidang
void main()
{
int pilih;
do{
clrscr();
cout<<"Menu Pilihan "<<endl;
cout<<"1. Diagonal bawah"<<endl;
cout<<"2. Diagonal atas"<<endl;
cout<<"3. Silang"<<endl;
cout<<"4. atas "<<endl;
cout<<"5. bawah "<<endl;
cout<<"6. bawah kiri"<<endl;
cout<<"7. bawah kanan"<<endl;
cout<<"8. Keluar"<<endl;
cout<<"Pilihan anda : ";cin>>pilih;
switch(pilih)
{
case 1:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i==j)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 2:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i+j==11)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 3:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if((i==j)||(i+j==11))
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 4:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i+j<=11)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 5:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i+j>=11)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 6:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i>j)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
case 7:
{
for(int i=1;i<=10;i++)
{ for(int j=1;j<=10;j++)
if(i<j)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
getch();
break;
}
}while(pilih!=8);
}
getch();
}
Membuat Persegi Panjang |
Program :
#include <iostream.h>
#include <conio.h>
//MEMBUAT PERSEGI PANJANG
void main()
{
int i,j,n;
cout<<"program membuat persegi"<<endl;
cout<<"======================="<<endl;
cout<<endl;
cout<<"Masukkan ukuran persegi : "; cin>>n;
for (i=1;i<=n;i++)
{
if ((i==1)||(i==n))
{
for (j=1;j<=n;j++)
cout<<"*";
cout<<endl;
}
else
{
for (j=1;j<=n;j++)
{
if ((j==1)||(j==n))
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
}
getch();
}
Source: www.gatewan.com
0 Response to "Contoh PROGRAM PERULANGAN C++"
Posting Komentar