Anda di halaman 1dari 14

/*************************

Program Input - Output


**************************/

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

using namespace std;

int main()
{
int dataku,datamu;

cout<<"Unusida"<<endl<<endl; //menampilkan ke layar

dataku = -100;
cout<<"Dataku = "<<dataku<<endl<<endl;

cout<<"Inputkan Datamu : ";


cin>>datamu; //input lewat keyboard
cout<<"Datamu = "<<datamu;

_getch();
}

1
/************************
Program Penjumlahan
*************************/

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

using namespace std;

int main()
{
int angka1,angka2,hasil;

cout<<"PROGRAM PENJUMLAHAN"<<endl;
cout<<"-------------------"<<endl;
cout<<"Inputkan bilangan pertama : ";
cin>>angka1; //input angka1
cout<<"Inputkan bilangan kedua : ";
cin>>angka2; //input angka2
hasil = angka1 + angka2; //hasil<--angka1 + angka2
cout<<angka1<<" + "<<angka2<<" = "<<hasil;

_getch();
}

2
/************************************
Program Luas Segitiga Siku-Siku
*************************************/

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

using namespace std;

int main()
{
int panjang,lebar;
float luas;

cout<<"PROGRAM SEGITIGA"<<endl;
cout<<"----------------"<<endl;
cout<<"Panjang : ";
cin>>panjang; //input panjang
cout<<"Lebar : ";
cin>>lebar; //input lebar
luas = (panjang * (float)lebar)/2; //luas<--(panjang * lebar)/2
cout<<"Luas : "<<luas;

_getch();
}

3
/*********************************************
Program Deteksi Bilangan Negatif-Positif
**********************************************/

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

using namespace std;

int main()
{
int angka;

puts("DETEKSI BILANGAN");
puts("----------------");
cout<<"Inputkan angka bukan nol : "; //input angka
cin>>angka;

/*-------- bentuk tunggal --------*/

cout<<endl<<"IF bentuk tunggal"<<endl;


cout<<"-----------------"<<endl;
if(angka < 0)
cout<<"NEGATIF";
if(angka > 0)
cout<<"POSITIF";

/*-------- bentuk ganda --------*/

cout<<endl<<endl<<"IF bentuk ganda"<<endl;


cout<<"---------------"<<endl;
if(angka < 0)
cout<<"NEGATIF";
else
cout<<"POSITIF";

_getch();
}

4
/******************************************
Program Deteksi Bilangan Ganjil-Genap
*******************************************/

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

using namespace std;

int main()
{
int angka,sisa;

puts("DETEKSI BILANGAN GANJIL-GENAP");


puts("-----------------------------");
cout<<"Inputkan angka bukan nol : "; //input angka
cin>>angka;

if(angka % 2 == 0)
cout<<angka<<" adalah bilangan GENAP";
else
cout<<angka<<" adalah bilangan GANJIL";

_getch();
}

5
/*****************************
Program Deteksi Bilangan
******************************/

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

using namespace std;

int main()
{
int angka,sisa;

puts("DETEKSI BILANGAN");
puts("----------------");
cout<<"Inputkan angka : "; //input angka
cin>>angka;

if(angka < 0) //negatif


cout<<angka<<" adalah bilangan NEGATIF";
else if(angka > 0) //positif
{
sisa = angka % 2;
if(sisa == 0) //genap
cout<<angka<<" adalah bilangan GENAP";
else //ganjil
cout<<angka<<" adalah bilangan GANJIL";
}
else //angka==0 (nol)
cout<<angka<<" adalah bilangan NOL";

_getch();
}

6
/******************************
Program Looping
Menampilkan Bilangan 1-10
*******************************/

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

using namespace std;

int main()
{
int i;

/*-------- Looping For -----------*/

puts("Looping dengan For\n");


for(i=1;i<=10;i++)
cout<<i<<" ";

/*-------- Looping While -----------*/

puts("\n\nLooping dengan While\n");


i=1;
while(i<=10)
{
cout<<i<<" ";
i++;
}

/*-------- Looping Do-While -----------*/

puts("\n\nLooping dengan Do-While\n");


i=1;
do
{
cout<<i<<" ";
i++;
}while(i<=10);

_getch();
}

7
LOOPING FOR dan WHILE

i i<=10 cout<<i i++


1 (1<=10)? T 1 2
2 (2<=10)? T 2 3
3 (3<=10)? T 3 4
: : : :
9 (9<=10)? T 9 10
10 (10<=10)? T 10 11
11 (11<=10)? F
Output

LOOPING DO-WHILE

i cout<<i i++ i<=10


1 1 2 (2<=10)? T
2 2 3 (3<=10)? T
3 3 4 (4<=10)? T
: : : :
9 9 10 (10<=10)? T
(11<=10)?
10 10 11
F

8
/*******************************
Program Deret Genap-Ganjil
********************************/

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

using namespace std;

int main()
{
int i;

puts("DERET BILANGAN GENAP ANTARA 1-10");


puts("--------------------------------");

for(i=1;i<=10;i++)
if(i%2==0) cout<<i<<" ";

puts("\n\nDERET BILANGAN GANJIL ANTARA 1-10");


puts("---------------------------------");

for(i=1;i<=10;i++)
if(i%2==1) cout<<i<<" ";

_getch();
}

9
/*********************************************
Program Faktor Persekutuan Terbesar (FPB)
**********************************************/

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

using namespace std;

int main()
{
int Bil1,Bil2,Kecil,FPB,i;

cout<<"Bil1 = "; cin>>Bil1;


cout<<"Bil2 = "; cin>>Bil2;

/*--------- faktor bilangan pertama ---------*/


cout<<"\n\nFaktor "<<Bil1<<" = ";
for(i=Bil1;i>=1;i--)
if(Bil1 % i == 0) cout<<i<<" ";

/*--------- faktor bilangan kedua -----------*/


cout<<"\nFaktor "<<Bil2<<" = ";
for(i=Bil2;i>=1;i--)
if(Bil2 % i == 0) cout<<i<<" ";

/*--------- faktor persekutuan ------------*/


cout<<"\n\nFaktor Persekutuan "<<Bil1<<" dan "<<Bil2<<" = ";
if(Bil1 < Bil2) Kecil = Bil1;
else Kecil = Bil2;

for(i=Kecil;i>=1;i--)
if((Bil1%i==0)&&(Bil2%i==0)) cout<<i<<" ";

/*--------- FPB ------------*/


cout<<"\n\nFaktor Persekutuan Terbesar antara "<<Bil1<<" dan "<<Bil2<<
" = ";
if(Bil1 < Bil2) Kecil = Bil1;
else Kecil = Bil2;

FPB = 0;
i = Kecil;
while(FPB==0)
{
if((Bil1%i==0)&&(Bil2%i==0)) FPB = i;
i--;
}
cout<<FPB;

_getch();
}

10
11
/*************************************************
Program Kelipatan Persekutuan Terkecil (KPK)
**************************************************/

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

using namespace std;

int main()
{
int Bil1,Bil2,i,Besar,Kecil,KPK;

cout<<"Bil1 = "; cin>>Bil1;


cout<<"Bil2 = "; cin>>Bil2;

if(Bil1>Bil2)
{
Besar = Bil1;
Kecil = Bil2;
}
else
{
Besar = Bil2;
Kecil = Bil1;
}
/*---------- kelipatan Bil1 ---------*/
cout<<"\n\nKelipatan "<<Bil1<<" = ";
for(i=1;i<=10;i++)
cout<<Bil1*i<<" ";

/*---------- kelipatan Bil2 ---------*/


cout<<"\nKelipatan "<<Bil2<<" = ";
for(i=1;i<=10;i++)
cout<<Bil2*i<<" ";

/*---------- kelipatan persekutuan --------*/


cout<<"\n\nKelipatan Persekutuan "<<Bil1<<" dan "<<Bil2<<" = ";
for(i=1;i<=10;i++)
if((i*Besar)%Kecil==0) cout<<i*Besar<<" ";

/*--------------- KPK ----------------*/


cout<<"\n\nKelipatan Persekutuan Terkecil antara "<<Bil1<<" dan
"<<Bil2<<" = ";
KPK=0;
i=1;
while(KPK==0)
{
if((i*Besar)%Kecil==0) KPK=i*Besar;
i++;
}
cout<<KPK;

_getch();
}

12
13
/*******************************
Program Looping Bersarang
( Nested Loop )
********************************/

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

using namespace std;

int main()
{
int i,j;

cout<<" i j"<<endl;
cout<<"-------"<<endl;

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

_getch();
}

i i<=5 j j<=3 cout<<i cout<<j j++ i++


1 (1<=5)? T 1 (1<=3)? T 1 1 2
2 (2<=3)? T 1 2 3
3 (3<=3)? T 1 3 4
4 (4<=3)? F 2
2 (2<=5)? T 1 (1<=3)? T 2 1 2
2 (2<=3)? T 2 2 3
3 (3<=3)? T 2 3 4
4 (4<=3)? F 3
3 (3<=5)? T 1 (1<=3)? T 3 1 2
2 (2<=3)? T 3 2 3
3 (3<=3)? T 3 3 4
4 (4<=3)? F 4
4 (4<=5)? T 1 (1<=3)? T 4 1 2
2 (2<=3)? T 4 2 3
3 (3<=3)? T 4 3 4
4 (4<=3)? F 5
5 (5<=5)? T 1 (1<=3)? T 5 1 2
2 (2<=3)? T 5 2 3
3 (3<=3)? T 5 3 4
4 (4<=3)? F 6
6 (6<=5)? F

14

Anda mungkin juga menyukai