Anda di halaman 1dari 10

#1.

if dengan satu kondisi

#include <iostream>
using namespace std; int main ()
{
int umur; cout<<”Masukkan umur: ”;cin>>umur; if(umur<17)
cout<<“anda tidak boleh memiliki SIM!”;

return 0;
}

#2. if dengan dua kondisi

#include <iostream>
using namespace std; int main ()
{
int a,b,c;
cout<<Masukkan nilai 1 : “); cin>>a;
cout<<“Masukkan nilai 2 : “); cin>>b; c=(a<b) ? b : a;
cout<<c;

return 0;
}

#3 if else satu kondisi

#include <iostream>
using namespace std;
int main ()
{
int umur;
cout<<”Masukkan umur:
”;cin>>umur;
if(umur<17)
{
cout<<“anda tidak boleh
memiliki SIM!”;
}
else
{
cout<<“anda boleh memiliki
SIM!”;
}
return 0;
}

#4 if else satu kondisi

#include <iostream>
using namespace std;
int main ()
{
int bil;
cout<<”Masukkan bilangan:
”;cin>>umur;
if(bil>=0)
{
cout<<“Bilangan Cacah”;
}
else
{
cout<<“bukan Bilangan Cacah
”;
}
return 0;
}

#5. if else dua kondis

#include <iostream> using namespace std; int main ()


{
string gender; cout<<"masukkan gender : ";cin>>gender;
if((gender=="L")|| (gender=="l"))
{
cout<<"Laki-laki";
}else if((gender=="P")|| (gender=="p"))
{
cout<<"Perempuan";
}
else
{
cout<<"Inputan salah";
}
return 0;
}

#6. if else bersarang dua kondisi

#include <iostream> using namespace std; int main ()


{
int skor;
cout<<"masukkan skor : "; cin>>skor;
if((skor>=50)&& (skor<l00))
{
cout<<"rookie";
}else if((skor>=100)&& (skor<500))
{
cout<<"advandce";
} else if((skor>=500)&& (skor<=1000))
{
cout<<"expert";
}
else
{
cout<<"tidak ada peringkat";
}
return 0;
}

#7. if dengan bayak kondisi

#include <iostream> int main()


{
int n;
cout<<”masukkan peringkat: ”; cin>>n;
if((n==1)||(n==2)|| (n==3)|| (n==4)|| (n==5))
{
cout<<“selamat anda juara ke-”<<n;
} else
{
cout<<“maaf anda belum juara”;
}
return 0;
}

#8. if dengan bayak kondisi

#include <iostream> using namespace std; int main ()


{
int skor;
cout<<"masukkan skor : "; cin>>skor;
if((skor>=50)&& (skor<l00))
{
cout<<"rookie";
}else if((skor>=100)&& (skor<500))
{
cout<<"advandce";
} else if((skor>=500)&& (skor<=1000))
{
cout<<"expert";
}
else
{
cout<<"tidak ada peringkat";
}
return 0;
}

Looping "Perulangan"

#9. Flow kontrol dengan Switch Case

#include <iostream> using namespace std; int main ()


{
int juara;
cout<<“masukkan peringkat : ”; cout>>juara; switch(juara)
{
case 1 : cout<<“anda juara 1”;break;
case 2 : cout<<“anda juara 2”;break;
case 3 : cout<<“anda juara 3”;break;
default : cout<<“anda belum juara”;break;
}
return 0;
}

#10. Perulanagn for

#include <iostream> using namespace std; int main ()


{
for(int i=0;i<5;i++)
{
cout<<“modul 2 alprogn”;
}
return 0;
}

#11. For untuk deret bilangan

#include <iostream> using namespace std; int main ()


{
for(int i=1;i<=5;i++)
{
cout<<i<<“,”;
}
return 0;
}

#12. For untuk deret bilangan

#include <iostream> using namespace std; int main ()


{
for(int i=1;i<=5;i++)
{
cout<<i<<“,”;
}
return 0;
}

#13. For untuk penjumlahan deret bilangan

#include <iostream> using namespace std; int main ()


{
int i, j; for(i=1;i<=5;i++)
{
j+=i;
}
cout<<j; return 0;
}

#14. For untuk penjumlahan deret bilangan

include <iostream> using namespace std; int main ()


{
int i, j; for(i=1;i<=5;i++)
{
j+=i;
}
cout<<j; return 0;
}

#15. For untuk penjumlahan deret bilangan

#include <iostream> using namespace std; int main ()


{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<“*”;
}
cout<<endl;
}
return 0;
}

#16. perulangan bersarang dengan for

#include <iostream> using namespace std; int main ()


{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<“*”;
}
cout<<endl;
}
return 0;
}

#17. Perulangan bersarang dengan for menampilkan karakter

#include <iostream> using namespace std; int main ()


{
int i,j;
for (i=0;i<4;i++){ for (j=0;j<4;j++){
cout<<"("<<i<<","<<j<<"),";
}
cout<<endl;
}
return 0;
}
#18. Perulangan bersarang dengan "for"menampilkan pasangan angka

#include <iostream> using namespace std; int main ()


{
int x=0, y=0; while(x<3) {
y=y+3; x++;
}
cout<<y; return 0;
}

#19. Perulangan dengan "While "

#include <iostream> using namespace std; int main ()


{
int x=0, y=0; do {
y=y+3; x++;
}
while(x<3); cout<<y; return 0;
}

#20. Perulangan dengan " While " Program perkalian

#include <iostream> using namespace std; int main ()


{
int x=0, y=0; while(x<3) {
y=y+3; x++;
}
cout<<y; return 0;
}
#21. Perulangan dengan "do while"

#include <iostream> using namespace std; int main ()


{
int x=0, y=0; do {
y=y+3; x++;
}
while(x<3); cout<<y; return 0;
}

#22. Perulangan dengan "do" Program perkalian

#include <iostream> using namespace std; int main ()


{
int x=0, y=0; do {
y=y+3; x++;
}
while(x<3); cout<<y; return 0;
}

#23. Perulanngan deret bilangan

#include <iostream> using namespace std; int main ()


{
int i=0; awal:
i++;
cout<<i<<","; if(i<=8)
goto awal; else
goto akhir; akhir:
return 0;
}
#24. Looping dengan deret bilangan

#include <iostream> using namespace std; int main ()


{
int i=99, a;
cout<<"Masukkan nim : ";cin>>a;
awal: a++;
cout<<a<<","; if(i>=a)
goto awal; else
goto akhir; akhir: return 0;
}

#25. Perulangan deret bilangan

#include <iostream> using namespace std; int main ()


{
int i=99, a;
cout<<"Masukkan nim : ";cin>>a;
awal: a++;
cout<<a<<","; if(i>=a)
goto awal; else
goto akhir; akhir: return 0;
}

Anda mungkin juga menyukai