PG0 - Exercise Questions

Sequential

Q1-1

Multiply two variables and output the result.
Input

var a = 10
var b = 20
Output

Result: 200
Example Answer

var a = 10
var b = 20

var c = a * b
exit c

Q1-2

Output the average of two variables.
Input

var a = 10
var b = 20
Output

Result: 15
Example Answer

var a = 10
var b = 20

var c = (a + b) / 2
exit c

Q1-3

Output the factorial of 3.
Hint: The factorial of 3 is 1*2*3.
Output

Result: 6
Example Answer

var result = 1
result = result * 2
result = result * 3
exit result

Q1-4

Round off the units place of a variable x and output the result.
Assume the value of x is always positive.
Since PG0 cannot handle decimals, dividing by 10 and then multiplying by 10 will round down. To round up, add 5 before rounding down.
Input

var x = 16	// Try changing the value to see how it works
Output

Result: 20

Example Answer

var x = 16

x = x + 5
x = x / 10
x = x * 10
exit x

Example Answer (PG0.5)

var x = 16

exit int((x + 5) / 10) * 10

Q1-5

Output the result of adding tax (8%) to the variable money.
Hint: Since PG0 cannot handle decimals, calculate by multiplying by 100.
Input

var money = 3000	// Try changing the value to see how it works
Output

Result: 3240

Example Answer

var money = 3000

money = money * 108
money = money / 100
exit money

Example Answer (PG0.5)

var money = 3000

exit int(money * 1.08)

Q1-6

Swap and output the contents of an array.
Hint: Prepare a separate variable for swapping.
Input

var data = {10, 20}
Output

Result: {20,10}
Example Answer

var data = {10, 20}

var tmp = data[0]	// Temporarily store the first element of the array
data[0] = data[1]	// Move the second element to the first position
data[1] = tmp		// Place the temporary variable in the second position
exit 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 works
var b = 50	// Try changing the value to see how it works
Output

Result: 50
Example Answer

var a = 20
var b = 50

var result
if (a > b) {
	// If 'a' is larger
	result = a
} else {
	// If 'b' is larger
	result = 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 = -3

var result
if (x == 0) {
	// If x is 0
	result = 0
}
if (x > 0) {
	// If x is a positive value
	result = 1
}
if (x < 0) {
	// If x is a negative value
	result = -1
}
exit result

Example Answer (PG0.5)

var x = -3

var result
if (x > 0) {
	// If x is a positive value
	result = 1
} else if (x < 0) {
	// If x is a negative value
	result = -1
} else {
	// If x is 0
	result = 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 = -16

if (x < 0) {
	x = x - 5
} else {
	x = x + 5
}
x = x / 10
x = x * 10
exit x

Example Answer (PG0.5)

var x = -16

if (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 = 5

var result
if (x == 0) {
	// If x is 0
	result = 0
} else {
	if (x % 2 != 0) {
		// If x is an odd number
		result = 1
	}
	if (x % 2 == 0) {
		// If x is an even number
		result = 2
	}
}
exit result

Example Answer (PG0.5)

var x = 5

var result
if (x == 0) {
	// If x is 0
	result = 0
} else if (x % 2 != 0) {
	// If x is an odd number
	result = 1
} else {
	// If x is an even number
	result = 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 = 55

var result
if (point >= 61) {
	// If point is 61 or higher
	result = 3
}
if (point <= 60 && point >= 31) {
	// If point is between 60 and 31
	result = 2
}
if (point <= 30) {
	// If point is 30 or below
	result = 1
}
exit result

Example Answer (PG0.5)

var point = 55

var result
if (point >= 61) {
	// If point is 61 or higher
	result = 3
} else if (point <= 60 && point >= 31) {
	// If point is between 60 and 31
	result = 2
} else {
	// If point is 30 or below
	result = 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 works
var b = 20	// Try changing the value to see how it works
var c = 10	// Try changing the value to see how it works
Output

Result: 2

Example Answer

var a = 10
var b = 20
var c = 10

var result
if (a == b && b == c) {
	// If all three are the same
	result = 3
} else {
	if (a == b || b == c || a == c) {
		// If any two are the same
		result = 2
	} else {
		// If none match
		result = 0
	}
}
exit result

Example Answer (PG0.5)

var a = 10
var b = 20
var c = 10

var result
if (a == b && b == c) {
	// If all three are the same
	result = 3
} else if (a == b || b == c || a == c) {
	// If any two are the same
	result = 2
} else {
	// If none match
	result = 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 = 0
var i = 0
while (i <= 10) {		// Loop from 0 to 10
	total = total + i	// Add the loop counter to the total
	i = i + 1		// Increment the loop counter
}
exit total

Example Answer (PG0.5)

var total = 0
for (var i = 0; i <= 10; i++) {		// Loop from 0 to 10
	total += 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 = 0
var i = 0
while (i <= 10) {
	total = total + i
	i = i + 2	// Increment the loop counter by 2 to only process even numbers
}
exit total

Example Answer 2

var total = 0
var i = 0
while (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 = 0
for (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 = 1
var i = 2
while (i <= 9) {
	result = result * i
	i = i + 1
}
exit result

Example Answer (PG0.5)

var result = 1
for (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 works
var b = 100	// Try changing the value to see how it works
Output

Result: 4
Example Answer

var a = 124
var b = 100

// Euclidean algorithm
while (b != 0) {
	var r = a % b
	a = b
	b = 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 = 5

var total = 0
var i = 0
while (i < cnt) {	// Loop for the number of elements in the array
	total = total + data[i]	// Sum the values of the array
	i = i + 1
}
exit total

Example Answer (PG0.5)

var data = {2,57,30,100,65}

var total = 0
for (var i = 0; i < length(data); i++) {	// Loop for the number of elements in the array
	total += 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 = 5

var i = 0
var j = cnt - 1
while (j - i > 0) {
	// Swap the values
	var tmp = data[i]
	data[i] = data[j]
	data[j] = tmp
	i = i + 1
	j = 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.

a = {{1,2},{3,4}}

b[0] = {1,2}
b[1] = {3,4}

c[0][0] = 1
c[0][1] = 2
c[1][0] = 3
c[1][1] = 4
Output

Result: {{1,2,3,4,5,6,7,8,9},{2,4,6,8,10,12,14,16,18},{3,6,9,12,15,18,21,24,27},
{4,8,12,16,20,24,28,32,36},{5,10,15,20,25,30,35,40,45},{6,12,18,24,30,36,42,48,54},
{7,14,21,28,35,42,49,56,63},{8,16,24,32,40,48,56,64,72},{9,18,27,36,45,54,63,72,81}}

Example Answer

var result[]
var i = 0
while (i < 9) {
	var j = 0
	while (j < 9) {
		result[i][j] = (i + 1) * (j + 1)	// Add 1 to i and j as they start from 0
		j = 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 = 10

var min = data[0]
var i = 1
while (i < cnt) {
	if (min > data[i]) {
		// Store the smaller value in min
		min = 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 min
		min = 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 = 8

var result[]
var i = 0, j = 0
while (i < cnt) {
	if (data[i] % 2 != 0) {
		// If the number is odd (not divisible by 2)
		result[j] = data[i]	// Add to the new array
		j = 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 = 0
for (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 found
var i = 2
while (i <= value / 2 && value % i != 0) {
	i = i + 1
}
var result = 0
if (i > value / 2) {
	// If the loop did not end due to finding a divisor, it's a prime number
	result = 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
Output

Result: {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30},
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}}

Example Answer

var year = 2018
var Calendar[]
var day[] = {31,28,31,30,31,30,31,31,30,31,30,31} // Number of days in each month

// Leap year determination
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
	day[1] = 29
}

// Creating the calendar
var i = 0
while (i < 12) {
	var j = 0
	while (j < day[i]) {
		Calendar[i][j] = j + 1
		j = j + 1
	}
	i = i + 1
}
exit Calendar

Example Answer (PG0.5)

var year = 2018
var Calendar[]
var day[] = {31,28,31,30,31,30,31,31,30,31,30,31} // Number of days in each month

// Leap year determination
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
	day[1] = 29
}

// Creating the calendar
for (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 sort
var i = 0
while (i < cnt - 1) {
	var j = cnt - 1
	while (j > i) {
		if (data[j - 1] < data[j]) {
			// Swap values
			var 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 sort
var i = 0
while (i < cnt) {
	var max = i
	var j = i + 1
	while (j < cnt) {
		if (data[j] > data[max]) {
			// Find the largest value
			max = j
		}
		j = j + 1
	}
	if (max != i) {
		// Swap values
		var 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 = 7

var result = 0
var i = 0
while (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 = 0
for (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 digits
var x = data
var digit = 0
while (x > 0) {
	x = x / 10
	digit = digit + 1
}
// Set each digit to the array, starting from the least significant digit
var result[]
var i = digit - 1
while (i >= 0) {
	result[i] = data - (data / 10 * 10)
	data = data / 10
	i = i - 1
}
exit result

Example Answer (PG0.5)

var data = 2830050

// Calculate the number of digits
for (var x = data, digit = 0; x > 0; x = int(x / 10), digit++) {
}
// Set each digit to the array, starting from the least significant digit
var result[]
for (var i = digit - 1; i >= 0; i--) {
	result[i] = data - (int(data / 10) * 10)
	data = int(data / 10)
}
exit result


Return
Copyright © 1996-2023 by Ohno Tomoaki. All rights reserved.