var data = {10, 20}var tmp = data[0]// Temporarily store the first element of the arraydata[0] = data[1]// Move the second element to the first positiondata[1] = tmp// Place the temporary variable in the second positionexit data
Branching
The question uses branching (if statements).
Q2-1
Output the larger value of two variables.
Input
var a = 20// Try changing the value to see how it worksvar b = 50// Try changing the value to see how it works
Output
Result:50
Example Answer
var a = 20var b = 50var resultif (a > b) {// If 'a' is largerresult = a} else {// If 'b' is largerresult = b}exit result
Q2-2
Output 0 if the variable x is zero, 1 if it's a positive value, and -1 if it's a negative value.
Input
var x = -3// Try changing the value to see how it works
Output
Result:-1
Example Answer
var x = -3var resultif (x == 0) {// If x is 0result = 0}if (x > 0) {// If x is a positive valueresult = 1}if (x < 0) {// If x is a negative valueresult = -1}exit result
Example Answer (PG0.5)
var x = -3var resultif (x > 0) {// If x is a positive valueresult = 1} else if (x < 0) {// If x is a negative valueresult = -1} else {// If x is 0result = 0}exit result
Q2-3
Round off the units place of variable x and output the result.
Input
var x = -16// Try changing the value to see how it works
Output
Result:-20
Example Answer
var x = -16if (x < 0) {x = x - 5} else {x = x + 5}x = x / 10x = x * 10exit x
Example Answer (PG0.5)
var x = -16if (x < 0) {x -= 5} else {x += 5}exit int(x / 10) * 10
Q2-4
Output 0 if variable x is zero, 1 if it's an odd number, and 2 if it's an even number.
Input
var x = 5// Try changing the value to see how it works
Output
Result:1
Example Answer
var x = 5var resultif (x == 0) {// If x is 0result = 0} else {if (x % 2 != 0) {// If x is an odd numberresult = 1}if (x % 2 == 0) {// If x is an even numberresult = 2}}exit result
Example Answer (PG0.5)
var x = 5var resultif (x == 0) {// If x is 0result = 0} else if (x % 2 != 0) {// If x is an odd numberresult = 1} else {// If x is an even numberresult = 2}exit result
Q2-5
Output 3 if the variable point is 61 or higher, 2 if it's between 60 and 31, and 1 if it's 30 or below.
Hint: Use the '&&' operator for compound conditions in this question.
Input
var point = 55// Try changing the value to see how it works
Output
Result:2
Example Answer
var point = 55var resultif (point >= 61) {// If point is 61 or higherresult = 3}if (point <= 60 && point >= 31) {// If point is between 60 and 31result = 2}if (point <= 30) {// If point is 30 or belowresult = 1}exit result
Example Answer (PG0.5)
var point = 55var resultif (point >= 61) {// If point is 61 or higherresult = 3} else if (point <= 60 && point >= 31) {// If point is between 60 and 31result = 2} else {// If point is 30 or belowresult = 1}exit result
Q2-6
Output 3 if all three variables (a, b, c) are the same, 2 if two are the same, and 0 if none match.
Input
var a = 10// Try changing the value to see how it worksvar b = 20// Try changing the value to see how it worksvar c = 10// Try changing the value to see how it works
Output
Result:2
Example Answer
var a = 10var b = 20var c = 10var resultif (a == b && b == c) {// If all three are the sameresult = 3} else {if (a == b || b == c || a == c) {// If any two are the sameresult = 2} else {// If none matchresult = 0}}exit result
Example Answer (PG0.5)
var a = 10var b = 20var c = 10var resultif (a == b && b == c) {// If all three are the sameresult = 3} else if (a == b || b == c || a == c) {// If any two are the sameresult = 2} else {// If none matchresult = 0}exit result
Looping
The question uses looping (while statements).
Q3-1
Output the sum of numbers from 0 to 10.
Output
Result:55
Example Answer
var total = 0var i = 0while (i <= 10) {// Loop from 0 to 10total = total + i// Add the loop counter to the totali = i + 1// Increment the loop counter}exit total
Example Answer (PG0.5)
var total = 0for (var i = 0; i <= 10; i++) {// Loop from 0 to 10total += i// Add the loop counter to the total}exit total
Q3-2
Output the sum of even numbers from 0 to 10.
Output
Result:30
Example Answer 1
var total = 0var i = 0while (i <= 10) {total = total + ii = i + 2// Increment the loop counter by 2 to only process even numbers}exit total
Example Answer 2
var total = 0var i = 0while (i <= 10) {if (i % 2 == 0) {// If the number is even (divisible by 2)total = total + i}i = i + 1}exit total
Example Answer (PG0.5)
var total = 0for (var i = 0; i <= 10; i += 2) {total += i}exit total
Q3-3
Output the factorial of 9.
Output
Result:362880
Example Answer
var result = 1var i = 2while (i <= 9) {result = result * ii = i + 1}exit result
Example Answer (PG0.5)
var result = 1for (var i = 2; i <= 9; i++) {result *= i}exit result
Q3-4
Output the greatest common divisor (GCD) of two variables.
Hint: The GCD of 124 and 100 is 4, as 124 % 100 equals 24, 100 % 24 equals 4, and 24 % 4 equals 0.
Input
var a = 124// Try changing the value to see how it worksvar b = 100// Try changing the value to see how it works
Output
Result:4
Example Answer
var a = 124var b = 100// Euclidean algorithmwhile (b != 0) {var r = a % ba = bb = r}exit a
Q3-5
Output the sum of the contents of an array.
Input
var data = {2,57,30,100,65}var cnt = 5
Output
Result:254
Example Answer
var data = {2,57,30,100,65}var cnt = 5var total = 0var i = 0while (i < cnt) {// Loop for the number of elements in the arraytotal = total + data[i]// Sum the values of the arrayi = i + 1}exit total
Example Answer (PG0.5)
var data = {2,57,30,100,65}var total = 0for (var i = 0; i < length(data); i++) {// Loop for the number of elements in the arraytotal += data[i]// Sum the values of the array}exit total
Q3-6
Reverse the contents of an array and output the result.
Hint: Prepare two loop counters, one increasing from the front and the other decreasing from the back. The loop ends when these two counters swap places.
Input
var data = {2,57,30,100,65}var cnt = 5
Output
Result:{65,100,30,57,2}
Example Answer
var data = {2,57,30,100,65}var cnt = 5var i = 0var j = cnt - 1while (j - i > 0) {// Swap the valuesvar tmp = data[i]data[i] = data[j]data[j] = tmpi = i + 1j = j - 1}exit data
Q3-7
Store the results of a multiplication table in a two-dimensional array and output it.
Hint: Use nested loops, starting with the inner loop.
Two-dimensional arrays are considered separately.
The following are written differently for a, b and c but have the same value.
var result[]var i = 0while (i < 9) {var j = 0while (j < 9) {result[i][j] = (i + 1) * (j + 1)// Add 1 to i and j as they start from 0j = j + 1}i = i + 1}exit result
Example Answer (PG0.5)
var result[]for (var i = 0; i < 9; i++) {for (var j = 0; j < 9; j++) {result[i][j] = (i + 1) * (j + 1)// Add 1 to i and j as they start from 0}}exit result
Combination
The questions combine basic structures (sequential, branching and looping).
Q4-1
Output the smallest value in an array.
Input
var data = {3,4,56,25,47,72,2,23,25,9}var cnt = 10
Output
Result:2
Example Answer
var data = {3,4,56,25,47,72,2,23,25,9}var cnt = 10var min = data[0]var i = 1while (i < cnt) {if (min > data[i]) {// Store the smaller value in minmin = data[i]}i = i + 1}exit min
Example Answer (PG0.5)
var data = {3,4,56,25,47,72,2,23,25,9}var min = data[0]for (var i = 1; i < length(data); i++) {if (min > data[i]) {// Store the smaller value in minmin = data[i]}}exit min
Q4-2
Create an array containing only the odd numbers from another array and output it.
Hint: Use a new array to store the results.
Input
var data = {34,55,89,32,56,103,6,345}var cnt = 8
Output
Result:{55,89,103,345}
Example Answer
var data = {34,55,89,32,56,103,6,345}var cnt = 8var result[]var i = 0, j = 0while (i < cnt) {if (data[i] % 2 != 0) {// If the number is odd (not divisible by 2)result[j] = data[i]// Add to the new arrayj = j + 1}i = i + 1}exit result
Example Answer (PG0.5)
var data = {34,55,89,32,56,103,6,345}var result[]var j = 0for (var i = 0; i < length(data); i++) {if (data[i] % 2 != 0) {// If the number is odd (not divisible by 2)result[j++] = data[i]// Add to the new array}}exit result
Q4-3
Output 1 if the variable value is a prime number, otherwise output 0.
Input
var value = 23// Try changing the value to see how it works
Output
Result:1
Example Answer
var value = 23// Loop continues until a divisor is foundvar i = 2while (i <= value / 2 && value % i != 0) {i = i + 1}var result = 0if (i > value / 2) {// If the loop did not end due to finding a divisor, it's a prime numberresult = 1}exit result
Q4-4
Store and output the annual calendar in a two-dimensional array.
Input
var year = 2018// Try changing the value to see how it works
var year = 2018var Calendar[]var day[] = {31,28,31,30,31,30,31,31,30,31,30,31}// Number of days in each month// Leap year determinationif (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {day[1] = 29}// Creating the calendarvar i = 0while (i < 12) {var j = 0while (j < day[i]) {Calendar[i][j] = j + 1j = j + 1}i = i + 1}exit Calendar
Example Answer (PG0.5)
var year = 2018var Calendar[]var day[] = {31,28,31,30,31,30,31,31,30,31,30,31}// Number of days in each month// Leap year determinationif (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {day[1] = 29}// Creating the calendarfor (var i = 0; i < 12; i++) {for (var j = 0; j < day[i]; j++) {Calendar[i][j] = j + 1}}exit Calendar
Q4-5
Sort the contents of an array in descending order and output them.
Input
var data = {3,4,56,25,47,72,2,23,25,9}var cnt = 10
Output
Result:{72,56,47,25,25,23,9,4,3,2}
Example Answer 1
var data = {3,4,56,25,47,72,2,23,25,9}var cnt = 10// Bubble sortvar i = 0while (i < cnt - 1) {var j = cnt - 1while (j > i) {if (data[j - 1] < data[j]) {// Swap valuesvar tmp = data[j]data[j] = data[j - 1]data[j - 1] = tmp}j = j - 1}i = i + 1}exit data
Example Answer 2
var data = {3,4,56,25,47,72,2,23,25,9}var cnt = 10// Selection sortvar i = 0while (i < cnt) {var max = ivar j = i + 1while (j < cnt) {if (data[j] > data[max]) {// Find the largest valuemax = j}j = j + 1}if (max != i) {// Swap valuesvar tmp = data[i]data[i] = data[max]data[max] = tmp}i = i + 1}exit data
Q4-6
Convert the contents of the array data into a single integer, treating each element as a digit, and output the result.
Each element of the array data is a single-digit value.
Input
var data = {2,8,3,0,0,5,0}var cnt = 7
Output
Result:2830050
Example Answer
var data = {2,8,3,0,0,5,0}var cnt = 7var result = 0var i = 0while (i < cnt) {result = result * 10 + data[i]i = i + 1}exit result
Example Answer (PG0.5)
var data = {2,8,3,0,0,5,0}var result = 0for (var i = 0; i < length(data); i++) {result = result * 10 + data[i]}exit result
Q4-7
Split the value of the variable data into individual digits and store them in an array, then output the array.
The value of the variable data should be a positive number only.
Input
var data = 2830050// Try changing the value to see how it works
Output
Result:{2,8,3,0,0,5,0}
Example Answer
var data = 2830050// Calculate the number of digitsvar x = datavar digit = 0while (x > 0) {x = x / 10digit = digit + 1}// Set each digit to the array, starting from the least significant digitvar result[]var i = digit - 1while (i >= 0) {result[i] = data - (data / 10 * 10)data = data / 10i = i - 1}exit result
Example Answer (PG0.5)
var data = 2830050// Calculate the number of digitsfor (var x = data, digit = 0; x > 0; x = int(x / 10), digit++) {}// Set each digit to the array, starting from the least significant digitvar result[]for (var i = digit - 1; i >= 0; i--) {result[i] = data - (int(data / 10) * 10)data = int(data / 10)}exit result