welcome to my blog


WELCOME MY BLOGER

Rabu, 11 November 2009

Tugas Logika

Soal No 1.
#include
#include
#include
main()
{
char a;
a=1;
clrscr;
{
printf("1 2 3 4 5 \n");
printf("1 2 3 4 \n");
printf("1 2 3 \n");
printf("1 2\n");
printf("1\n");
a++;
}
while(a<=5);
getch();
}
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Soal No 2.
#include
#include
#include
main()
{
char a;
a=1;
clrscr;
{
printf("1 2 3 4 5 \n");
printf("6 7 8 9 10 \n");
printf("11 12 13 14 15 \n");
a++;
}
while(a<=5);
getch();
}
Output:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Soal No 3.
#include
#include
#include
main()
{
char a;
a=1;
clrscr;
{
printf("1 2 3 4 5 \n");
printf("6 7 8 9 \n");
printf("10 11 12 \n");
printf("13 14\n");
printf("15\n");
a++;
}
while(a<=5);
getch();
}
Output:
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Soal No 4.
#include
#include
#include
main()
{
char a;
a=1;
clrscr;
{
printf("2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110\n");
printf("Bilangan Genap\n");
a++;
}
while(a<=5);
getch();
}
Output :
2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110
Bilangan Genap

Soal No 5.
#include
#include
#include
main()
{
char a;
a=1;
clrscr;
{
printf("1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100\n");
printf("Bilangan Ganjil\n");
a++;
}
getch();
}
Output:
1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100
Bilangan Ganjil

STRUKTUR LOOPING

1. Statement While

int bil=1;
while(bil<=5)
cout< ++bil; bil = bil + 1
Output: 12345

Jawab :

Bil = 1 Bil<=5 Cetak bil Bil= bil + 1
1 1<=5 1 2
2 2<=5 2 3
3 3<=5 3 4
4 4<=5 4 5
5 5<=5 5 6
6 6<=5

2. Statement Do….While

int bil=2;
do
cout<bil+=2;
while (bil>=10);
output: 2 4 6 8 10

Jawab:

Bil=2 Cetak bil Bil + 2 Bil>=10 Y/T
2 2 4 4>=10 T
4 4 6 6>=10 T
6 6 8 8>=10 T
8 8 10 10>=10 T
10 10 12 12>=10 Y

3. Statement FOR

Contoh 1.a
#include
Void main()
{
Int I,N;
N = 45;
I = 1;
While (I<=5);
{
Printf (“\n%i,”N);
N = N + 5
I = I + 1
}
}
Output: 45 50 55 60 65
Jawab:

N = 45 I = 1 I<=5 Y/T Cetak N N = N + 5 I = I + 1
45 1 1<=5 Y 45 50 2
50 2 2<=5 Y 50 55 3
55 3 3<=5 Y 55 60 4
60 4 4<=5 Y 60 65 5
65 5 5<=5 Y 65 70 6
70 6 6<=5 T

Contoh 1.b
#include
Void main()
{
Int I,N;
N = 45;
I = 1;
While (I<=5);
{
N = N + 5
I = I + 1
}
Printf (“\n%i,”N);
}

Output: 70
Jawab :

N = 45 I = 1 I<=5 Y/T N = N + 5 I = I + 1 Cetak N
45 1 1<=5 Y 50 2
50 2 2<=5 Y 55 3
55 3 3<=5 Y 60 4
60 4 4<=5 Y 65 5
65 5 5<=5 Y 70 6
70 6 6<=5 T 70


Contoh 1.c
#include
Void main()
{
Int I,N;
N = 45;
I = 1;
While (I<=5);
{
N = N + 5
Printf (“\n%i,”N);
I = I + 1
}
}
Output: 45 50 55 60 65
Jawab:

N = 45 I = 1 I<=5 Y/T N = N + 5 Cetak N I = I + 1
45 1 1<=5 Y 50 45 2
50 2 2<=5 Y 55 50 3
55 3 3<=5 Y 60 55 4
60 4 4<=5 Y 65 60 5
65 5 5<=5 Y 70 65 6
70 6 6<=5 T

Contoh 2
#include
Void main()
{
Int I,N;
N = 10;
I = 10;
While (N<=100);
{
N = N + I
Printf (“\n%i,”N);
I = I + 5;
}
}
Output: 20 35 55 80 110
Jawab:

N = 10 I = 1 N<=100 Y/T N = N + 1 Cetak N I = I + 5
10 10 10<=100 Y 20 20 15
20 15 15<=100 Y 35 35 20
35 20 20<=100 Y 55 55 25
55 25 25<=100 Y 80 80 30
80 30 30<=100 Y 110 110 35
110 35 35<=100 T



Contoh 3.a
#include
Void main()
{
Int I,N;
N = 10;
X = 10;
T = 0;
While (T<=100);
{
T = T + N;
N = N + X;
X = X + 5;
}
Printf (“\n%i,”T);
}
Output: 120


Jawab:

N=10 X=10 T=0 T<=100 Y/T T =T+N N =N+X X=X+5 Cetak T
10 10 0 0<=100 Y 10 20 15
20 15 10 10<=100 Y 30 35 20
35 20 30 30<=100 Y 65 55 25
55 25 65 65<=100 Y 120 80 30
80 30 120 120<=100 T 120


Contoh 3.b
#include
Void main()
{
Int I,N;
N = 10;
X = 10;
T = 0;
While (T<=100);
{
T = T + N;
Printf (“\n%i,”T);
N = N + X;
X = X + 5;
}
}
Output: 10 30 65 120

Jawab:

N=10 X=10 T=0 T<=100 Y/T T =T+N Cetak T N =N+X X=X+5
10 10 0 0<=100 Y 10 10 20 15
20 15 10 10<=100 Y 30 30 35 20
35 20 30 30<=100 Y 65 65 55 25
55 25 65 65<=100 Y 120 120 80 30
80 30 120 120<=100 T

4. Nested FOR

Contoh 1.a
int I.J;
for (I = 1, 1<=3;I++);
{
for(J=1;J<=5;J++);
{
Printf(“\n%i”,I);
}
}
Output: 1 1 1 1 1
2 2 2 2 2
3 3 3 3 3

Jawab:

I=1 I<=3 Y\T J=1 J<=5 Y\T Cetak I J++ I++
1 1<=3 Y 1 1<=5 Y 1 2
2 2<=5 Y 1 3
3 3<=5 Y 1 4
4 4<=5 Y 1 5
5 5<=5 Y 1 6
6 6<=5 T 2
2 2<=3 Y 1 1<=5 Y 2 2
2 2<=5 Y 2 3
3 3<=5 Y 2 4
4 4<=5 Y 2 5
5 5<=5 Y 2 6
6 6<=5 T 3
3 3<=3 Y 1 1<=5 Y 3 2
2 2<=5 Y 3 3
3 3<=5 Y 3 4
4 4<=5 Y 3 5
5 5<=5 Y 3 6
6 6<=5 T 4
4 4<=3 T

Contoh 1.b
int I.J;
for (I = 1, 1<=3;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