Sie sind auf Seite 1von 4

Nama : Muhammad Danang Adhiwijaya

Kelas : 2SC2

Tugas : Exercise 1 (hal. 1.31)

EXERCISE 1

1) Exercise 1
a. Accept a number from the user. Num it is num.
b. Declare an integer variable i.
c. Assign a value 2 to i.
d. Repeat until I > num/2:
a) if num % i = 0:
1. Display The number is not prime
2. Exit.
b) i = i + 1.
e. Display The number is prime
2) Exercise 2
a. Declare an integer variable, count, and assign the value 0 to it.
b. Declare an integer variable, num, and assign the value 2 to it.
c. Repeat until count becomes equal to 10.
a) if num is a prime number:
1. Display num.
2. Set count = count + 1.
b) Set num = num +1.
3) Exercise 3
- Accept a number in the range of 1 9 from the user. Store it in a
variable, num.
- Declare an integer variable, i.
- Set I = 1.
- Repeat until I becomes greater than num://To display //num

Rows
a. Declare an integer variable j
b. Set j = i
c. Display j and insert a space
d. Set j = j 1
e. If j > 0 go to step c
f. Insert a line break
g. Set I = I + 1
4) Exercise 4
1. Accept a number from the user in the range of 1 9. Store iit in a
variable, n.
2. Set I = 1.
3. Repeat until I becomes greater than n:
- Set j = 0
- Insert a space
- Set j = j + 1
- If j < n I, go to step b. //To display n I spaces
- Set j = 1
- Display the value of j and insert a space
- Set j = j + 1
- If j <I, than go to step f. //To display number from 1 to i
- Set j = i 1
- Display the value of j and insert a space
- Set j = j 1
- If j > 0, go to step j. //To display number from I 1 down.
//to 1
- Give a ine break
- Set I = i + 1
5) Exercise 5
1. Accept a string value from the user. Store it in a variable, str.
2. Accept the substring to be searched in str. Store it in a variable,
substr.
3. Store the length of str in an integer variable, len1.
4. Store the length of substr in an integer variable, len2.
5. Set i = 0.
6. Repeat until i becomes equal to len1:
- If str[i] != substr[0], go to step b else go to step d. //To find
the first matching character
- Set i = i + 1.
- If (i < len1) go to step a.
- If i = len1, go to step e, else go to step g.
- Display Substring no found in the string.
- Exit.
- Set j = i. //First character matched. Now match the. //
remaining character.
- Set k = 0.
- If str[j] = substr[k], go to step j, else go to step m.
- Set j = j + 1.
- Set k = k + 1.
- If j < len1 and k < len2, go to step i.
- If k = len2, go to step n, else go to step p.
- Display Substring found at position + (i + 1) + in the
string
- Exit.
- Set i = i + 1.
6) Exercise 6
- Accept two arrays, A1 and A2, each of size 10, from the user.
- Declare an array, result, of size 20.
- Set i = j = k = 0.
- If (A1 [i] <= A2 [j]): //Insert the smaller element in the. //result
array.
a. Result [k] = A1 [i].
b. Set k = k + 1.
c. Set i = i + 1.
- If (A1 [i] > A2 [j]): //Insert the smaller element in the. //result
array.
a. Result [k] = A2 [j].
b. Set k = k = 1.
c. Set j = j + 1.
- If (i < 10 and j < 10), go to step 4. //If none of the lists. //has
reached its. End.
- Repeat until i equals 10: //If there are any elements left. //in A1,
copy them to. //array.
Result
a. Result [k] = A1 [i].
b. Set k = k + 1.
c. Set i = i + 1.
- Repeat until j equals 10: //If there are any elements left in. //A2,
copy them to the result. //array.
a. Result [k] = A2 [j].
b. Set k = k + 1.
c. Set j = j + 1.
7) Exercise 7
1. Accept three numbers from the user. Store them in variable
num1, num2, and num3.
2. Declare an integer variable, min.
3. Assign the value of the smallest number among num1, num2,
and num3 t min by executing the following steps:
a. Set min = num1.
b. If (num2 < min), set min = num2.
c. If (num3 < min), set min = num3.
4. Declare an integer variable i.
5. Set i = min.
6. If (num1 % i = 0 and num2 % i = 0 and num3 % i = 0):
Display i. //If I devides all the numbers, then HCF is i.
Exit.

(Contd.)

1. Set i = i 1.
2. Go to step 6.
8) Exercise 8
1. Declare two 3 x 3 arrays, m1 and m1.
2. Accept the elements of the two matrices and store them in m1
and m2.
3. Delare a 3 x 3 matrix, result, to store the result of multiplication.
4. Set i = 0.
5. Set j = 0.
6. Set result [i, j] = 0.
7. Set k = 0.
8. Result [i, j] + m1 [i, k] x m2 [k, j].
9. Set k = k + 1.
10. If k < 3, go to step 8.
11. Set j = j + 1.

(contd.)
1. If j < 3, go to step 6.
2. Set i = i + 1.
3. If i < 3, go to step 5.
4. Display result.
9) Exercise 9.
1. Algoritm: Fibo (n).
2. If n = 1, return 0.
3. If n = 2, return 1.
4. Return (Fibo (n 1) + Fibo (n 2).

Das könnte Ihnen auch gefallen