Free for everyone · No sign-up needed

Learn to code.

75 beginner problems from real Fun Program Battle wins. Try each one, then check the approach and a tested Java solution.

75

Beginner problems

7

Topics, easy to harder

100%

Solutions compiled & tested

2,900+

Student battles reviewed

  1. 1 Read the problem & sample
  2. 2 Write your own program
  3. 3 Reveal the approach & solution

Showing 75 of 75 problems · 0 learned

Topic 1 · 6 problems

Basics

Input, output, formulas and if-else. Your first programs.

1 Odd or Even Easy if-else modulus (%)

Write a program that takes an integer as input and prints "Odd" if the number is odd, otherwise "Even". The program should work for both positive and negative integers.

Sample input
7
Sample output
Odd

Tip: Test with n % 2 == 0, not n % 2 == 1. In Java, -3 % 2 is -1, so the second check would wrongly call -3 "Even".

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
2 Smallest of Three Easy if-else comparison

Write a program to read three different integers and print the smallest of them. For example, if the input is 10, 20 and 5, the output should be 5.

Sample input
10 20 5
Sample output
Smallest number = 5

Tip: Java can also do this in one line with Math.min(Math.min(a, b), c), but writing the comparisons yourself is the better practice.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
3 Leap Year Easy logical operators if-else

Write a program to check whether a given year is a leap year. Print "Leap Year" if it is, otherwise print "Not a Leap Year". A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400.

Sample input
2024
Sample output
Leap Year

Tip: 1900 is divisible by 4 but is NOT a leap year, because it is divisible by 100 and not by 400. 2000 is a leap year. Always test these two years.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
4 Taxi Fare Easy arithmetic double formatting

Write a program to calculate the total fare for a taxi ride. The base fare is $2.50, plus $0.25 per mile travelled and $0.50 per minute of travel time. Input the distance in miles and the time in minutes, then display the total fare.

Sample input
10
20
Sample output
Total fare = 15.00

Tip: Use double for money values and String.format("%.2f", value) to always show two decimal places.

Try writing it yourself first!
Solved by 4 students in Fun Program Battle
5 Compound Interest Easy formula Math.pow double

Write a program to calculate the amount an investment grows to. Input the principal P, the annual rate of interest R (as a percentage) and the number of years N. Interest is compounded annually, so the amount is A = P x (1 + R/100)^N. Print the final amount.

Sample input
10000
10
2
Sample output
Amount after 2 years = 12100.00

Tip: Math.pow(x, y) returns x raised to the power y as a double.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
6 Sum from 1 to N Easy for loop accumulator

Write a program that takes a positive integer n as input and calculates the sum 1 + 2 + 3 + ... + n. For example, if the input is 5 the output is 15. If the input is not positive, print an error message.

Sample input
5
Sample output
Sum = 15

Tip: The formula n * (n + 1) / 2 gives the same answer. Use it to check your loop.

Try writing it yourself first!
Solved by 5 students in Fun Program Battle

Topic 2 · 9 problems

Loops & Patterns

for and while loops, number series and patterns.

7 Fibonacci Series Easy while loop series

The Fibonacci series starts with 0 and 1, and every next term is the sum of the previous two (0, 1, 1, 2, 3, 5, 8, ...). Write a program that takes an integer n as input and prints all Fibonacci numbers that are less than or equal to n. For example, if n is 10 the output is 0 1 1 2 3 5 8.

Sample input
10
Sample output
0 1 1 2 3 5 8

Tip: You only need two variables for the last two terms. The third variable is just a temporary holder while shifting them forward.

Try writing it yourself first!
Solved by 15 students in Fun Program Battle
8 Triangular Numbers Easy while loop series counting

A triangular number is the sum of the first k natural numbers: T(k) = k x (k + 1) / 2, giving 1, 3, 6, 10, 15, ... Write a program that takes an integer n and prints all triangular numbers up to n, followed by how many there are.

Sample input
20
Sample output
1 3 6 10 15
Count = 5

Tip: Each triangular number is the previous one plus the next k, so you can also build them as t = t + k without the formula.

Try writing it yourself first!
Solved by 12 students in Fun Program Battle
9 Perfect Squares Easy for loop series sum

A perfect square is the square of an integer: 1, 4, 9, 16, ... Write a program to read an integer n, print all perfect squares less than or equal to n separated by commas, and also print their sum.

Sample input
30
Sample output
1, 4, 9, 16, 25
Sum = 55

Tip: Loop over i (the root) instead of testing every number up to n. The condition i * i <= n stops the loop at the right time.

Try writing it yourself first!
Solved by 25 students in Fun Program Battle
10 Odd Squares Sum Easy for loop step of 2

Write a program that calculates the sum of the squares of all odd numbers less than a given integer N. For example, if N = 6 the odd numbers are 1, 3 and 5, so the output is 1 + 9 + 25 = 35.

Sample input
6
Sample output
Sum = 35

Tip: Start the loop at 1 and step by 2 (i += 2) so that it only visits odd numbers. There is no need for an if inside the loop.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
11 Factorial Digit Sum Medium factorial long digit extraction

Write a program that takes a non-negative integer n, calculates n! (n factorial), and then finds the sum of the digits of the result. For example, for n = 5 the factorial is 120 and the digit sum is 1 + 2 + 0 = 3. Accept values of n from 0 to 20.

Sample input
5
Sample output
Factorial = 120
Sum of digits = 3

Tip: Factorials grow very fast. int overflows after 12! and long after 20!, so this program uses long and limits n to 20. (For bigger values Java has BigInteger.) Remember that 0! = 1.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
12 Floyd's Triangle Easy nested loops patterns

Write a program that reads the number of rows n and prints a triangle of numbers where row 1 has one number, row 2 has two numbers, and so on. The numbers keep counting up from 1. For n = 4 the output is: 1 2 3 4 5 6 7 8 9 10

Sample input
4
Sample output
1
2 3
4 5 6
7 8 9 10

Tip: In almost every pattern program the outer loop controls the rows and the inner loop controls what is printed in each row.

Try writing it yourself first!
Solved by 6 students in Fun Program Battle
13 Growing Pattern Easy nested loops patterns

Write a program that reads a starting number s and the number of rows r, and prints a pattern where each row starts again from s and has one more number than the previous row. For s = 5 and r = 4 the output is: 5 5 6 5 6 7 5 6 7 8

Sample input
5
4
Sample output
5
5 6
5 6 7
5 6 7 8

Tip: Compare this with Floyd's triangle. There the counter keeps running across rows. Here it is reset to s at the start of every row.

Try writing it yourself first!
Solved by 6 students in Fun Program Battle
14 Odd Pyramid Medium nested loops patterns spacing

Write a program that takes the height h of a pyramid and prints consecutive odd numbers in a centred pyramid shape: row 1 has one odd number, row 2 has the next two, and so on. For h = 3 the output is: 1 3 5 7 9 11

Sample input
3
Sample output
  1
 3 5
7 9 11

Tip: Centring comes from printing (h - i) spaces before row i. Print the spaces first, then the numbers.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
15 Up and Down Easy for loop reverse loop

Write a program that takes an integer n and prints the numbers from 1 to n, followed by the same numbers in reverse order, all on one line. For example, if the input is 3 the output is 1 2 3 3 2 1.

Sample input
3
Sample output
1 2 3 3 2 1

Tip: A reverse loop starts at n, keeps going while i >= 1, and uses i--.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle

Topic 3 · 24 problems

Digits

Take numbers apart with % 10 and / 10. The most popular battle topic.

16 Prime Digits Easy digit extraction while loop

Write a program that takes a positive integer and counts how many of its digits are prime. The prime digits are 2, 3, 5 and 7. For example, for 237 the output is 3, because all three digits are prime.

Sample input
237
Sample output
Prime digits = 3

Tip: This is the key digit-extraction trick used throughout this section: n % 10 gives the last digit and n / 10 removes it. Repeat until n becomes 0.

Try writing it yourself first!
Solved by 24 students in Fun Program Battle
17 Odd Digits Easy digit extraction modulus (%)

Write a program to count the number of odd digits in a given integer. For example, 12345 has three odd digits (1, 3 and 5), so the output is 3. For 24680 the output is 0.

Sample input
12345
Sample output
Odd digits = 3

Tip: A digit d is odd when d % 2 != 0. Use Math.abs so that negative numbers work too.

Try writing it yourself first!
Solved by 7 students in Fun Program Battle
18 Odd & Even Digits Easy digit extraction do-while

Write a program that takes a positive integer and counts how many of its digits are odd and how many are even. For example, for 1234 the output is: odd digits = 2, even digits = 2.

Sample input
1234
Sample output
Odd digits = 2
Even digits = 2

Tip: A do-while loop runs at least once, so the input 0 is correctly counted as one even digit. A plain while (n > 0) loop would count nothing.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
19 Even Digit Sum Easy digit extraction sum

Write a program that calculates the sum of all even digits in a given integer. The number may be positive or negative. For example, for 123456 the output is 12 (2 + 4 + 6). If there are no even digits, the output is 0.

Sample input
123456
Sample output
Sum of even digits = 12

Tip: 0 is an even digit too. It just adds nothing to the sum.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
20 Even Digit Product Easy digit extraction product flag

Write a program that takes a positive integer and calculates the product of all its even digits. If the number has no even digits, print 1. For example, for 123456 the output is 48 (2 x 4 x 6), and for 13579 the output is 1. A 0 digit is even, so it makes the product 0.

Sample input
123456
Sample output
Product of even digits = 48

Tip: For a product, start at 1, not 0. Starting at 0 would make every answer 0.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
21 Digit Squares Sum Easy digit extraction sum

Write a program that takes a positive integer and calculates the sum of the squares of its digits. For example, for 123 the squares are 1, 4 and 9, so the output is 1 + 4 + 9 = 14.

Sample input
123
Sample output
Sum of squares of digits = 14

Tip: Multiply d * d rather than calling Math.pow(d, 2). It is simpler and stays an int.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
22 Single-Digit Product Easy digit extraction product if-else

Write a program to find the product of the digits of a positive integer and check whether that product is a single-digit number. For example, for 123 the product is 1 x 2 x 3 = 6, which is a single digit. For 99 the product is 81, which is not.

Sample input
123
Sample output
Product of digits = 6
Single digit

Tip: A non-negative number is a single digit when it is less than 10.

Try writing it yourself first!
Solved by 8 students in Fun Program Battle
23 Digit Difference Easy digit extraction two sums

Write a program that splits the digits of an integer into two groups: digits that are multiples of 3 (0, 3, 6, 9) and all the others. Print (sum of the other digits) - (sum of the multiples of 3). For example, for 1234 the multiple of 3 is 3 and the others are 1 + 2 + 4 = 7, so the output is 7 - 3 = 4.

Sample input
1234
Sample output
Difference = 4

Tip: Keep two separate running totals in one pass over the digits, then subtract at the end.

Try writing it yourself first!
Solved by 28 students in Fun Program Battle
24 Reverse & Sum Easy reverse number digit sum

Write a program that takes a positive integer and (1) reverses its digits and (2) finds the sum of its digits. Print both. If the input is not a positive integer, print an error message. For example, for 1234 the reversed number is 4321 and the digit sum is 10.

Sample input
1234
Sample output
Reversed number = 4321
Sum of digits = 10

Tip: To reverse a number, rev = rev * 10 + digit shifts the digits collected so far one place left and adds the new digit at the end.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
25 Bigger Reverse Easy reverse number comparison

Write a program that takes a positive integer, reverses its digits, and checks whether the reversed number is greater than the original. For example, 123 reversed is 321, which is greater, so the program reports that the reverse is greater.

Sample input
123
Sample output
Reversed number = 321
The reversed number is greater

Tip: Keep a copy of the original number. The reversing loop destroys n as it goes.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
26 Palindrome Number Easy reverse number palindrome

A palindrome number reads the same forwards and backwards. Write a program that checks whether a given integer is a palindrome. For example, 121 is a palindrome, while 123 is not.

Sample input
121
Sample output
121 is a palindrome

Tip: A palindrome is just a number that equals its own reverse, so this reuses the reversing loop from the previous problems.

Try writing it yourself first!
Solved by 9 students in Fun Program Battle
27 Digital Root Easy nested loops digit sum

The digital root of a number is found by adding its digits, then adding the digits of the result, and so on until a single digit remains. For example, the digital root of 16 is 1 + 6 = 7, and the digital root of 942 is 9 + 4 + 2 = 15, then 1 + 5 = 6. Write a program that prints the digital root of a given integer.

Sample input
942
Sample output
Digital root = 6

Tip: The outer loop repeats while the number has more than one digit (n > 9). The inner loop adds up the digits once.

Try writing it yourself first!
Solved by 22 students in Fun Program Battle
28 Magic Number Easy digital root nested loops

A magic number is a number that becomes 1 when you keep adding its digits until a single digit is left. For example, 19 is a magic number because 1 + 9 = 10 and 1 + 0 = 1. Write a program that checks whether a given number is a magic number and shows each step of the reduction.

Sample input
19
Sample output
19 -> 10 -> 1
19 is a Magic Number

Tip: This is the definition used in ICSE. A magic number is simply a number whose digital root is 1.

Try writing it yourself first!
Solved by 20 students in Fun Program Battle
29 Digit Product Chain Medium nested loops digit product

Write a program that takes a positive integer and multiplies its digits. If the product has more than one digit, multiply the digits of the product, and repeat until a single digit is left. Print each step and the final digit. For example, 39 gives 3 x 9 = 27, then 2 x 7 = 14, then 1 x 4 = 4, so the output is 4.

Sample input
39
Sample output
39 -> 27 -> 14 -> 4
Result = 4

Tip: This is the same structure as the digital root, but with multiplication. Remember to start each product at 1.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
30 Digit Frequency Medium arrays as counters digit extraction

Write a program that takes a positive integer n and prints how many times each digit 0 to 9 occurs in it, one digit per line. If n is less than 1, print an error message. For example, for 122333 the output shows 1: 1, 2: 2, 3: 3, and 0 for every other digit.

Sample input
122333
Sample output
0: 0
1: 1
2: 2
3: 3
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0

Tip: An array int[10] works as ten counters at once. count[d]++ increases the counter for digit d.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
31 Triple Digits Medium digit extraction tracking previous value

Write a program that checks whether a positive integer contains three or more identical digits in a row. For example, 122333 contains "333", so the output is "Yes". 112233 has no such run, so the output is "No".

Sample input
122333
Sample output
Yes

Tip: Remember the previous digit and the length of the current run. When the new digit equals the previous one the run grows, otherwise it restarts at 1.

Try writing it yourself first!
Solved by 6 students in Fun Program Battle
32 Target Digit Sum Medium search loop break digit sum

Write a program to find the smallest positive number whose digits add up to N. Search the numbers from 1 to 1,000,000. For example, for N = 10 the answer is 19 (1 + 9 = 10). If no such number exists in the range, print a message.

Sample input
10
Sample output
Smallest number = 19

Tip: Search from 1 upwards and break at the first match, which is automatically the smallest. The largest digit sum below 1,000,000 is 54 (999999), so a larger N has no answer.

Try writing it yourself first!
Solved by 46 students in Fun Program Battle
33 Multiples Digit Sum Easy for loop if digit sum

Write a program that takes three integers A, B and K, and finds the total sum of the digits of all numbers from A to B (inclusive) that are divisible by K. For example, for A = 1, B = 10 and K = 2 the numbers are 2, 4, 6, 8 and 10, so the total is 2 + 4 + 6 + 8 + (1 + 0) = 21.

Sample input
1
10
2
Sample output
Total digit sum = 21

Tip: Copy the loop variable into a temporary variable before extracting its digits. Changing i itself would break the loop.

Try writing it yourself first!
Solved by 63 students in Fun Program Battle
34 Lucky Numbers Easy for loop digit sum multiples

In this problem a number is called "lucky" if the sum of its digits is a multiple of 8. For example, 17 is lucky because 1 + 7 = 8. Write a program that takes a start and an end value and prints all lucky numbers between them (inclusive).

Sample input
1
50
Sample output
8 17 26 35 44

Tip: "Lucky" is a made-up rule for this problem only. ICSE also has a different, sieve-based "lucky number", so don't mix the two up.

Try writing it yourself first!
Solved by 29 students in Fun Program Battle
35 Armstrong Number Medium counting digits powers

An Armstrong number is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 153 has 3 digits and 1^3 + 5^3 + 3^3 = 153, so it is an Armstrong number. Write a program that checks whether a given positive integer is an Armstrong number.

Sample input
153
Sample output
153 is an Armstrong number

Tip: You need two passes over the digits: first to count them, then to add the powers. Raising to a power with a small loop avoids the double rounding that Math.pow can bring.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
36 Special Number Medium factorial String digits

A Special number (also called a Krishnamurthy number) equals the sum of the factorials of its digits. For example, 145 = 1! + 4! + 5! = 1 + 24 + 120. Write a program that takes a positive integer, shows the factorial sum of its digits, and says whether it is a Special number.

Sample input
145
Sample output
1! + 4! + 5! = 145
Yes, 145 is a Special number

Tip: Converting the number to a String lets you read its digits from left to right with charAt(i) - '0'. This keeps the printed breakdown in the right order.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
37 Swap & Prime Easy two-digit numbers prime check

Write a program that reads a two-digit number, swaps its two digits, and checks whether the new number is prime. For example, 13 becomes 31, which is prime. 12 becomes 21 = 3 x 7, which is not prime.

Sample input
13
Sample output
Swapped number = 31
31 is prime

Tip: For a two-digit number, n / 10 is the tens digit and n % 10 is the units digit. Note that 1 is not a prime number.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
38 Next Palindromes Medium palindrome counting loop

Write a program that takes a starting number and a count N, and prints the first N palindromic numbers that are greater than or equal to the starting number. A palindromic number reads the same when reversed. For example, from 123 with N = 5 the output is 131, 141, 151, 161 and 171.

Sample input
123
5
Sample output
131 141 151 161 171

Tip: When you don't know in advance how many numbers you will test, use a while loop that stops once you have found N matches.

Try writing it yourself first!
Solved by 4 students in Fun Program Battle
39 Unique Digit Numbers Medium boolean array nested loops

Write a program to count the numbers from 1 to N whose digits are all different (no digit repeats). For example, for N = 25 every number qualifies except 11 and 22, so the output is 23.

Sample input
25
Sample output
Count = 23

Tip: A boolean array seen[10] records which digits have already appeared. Make a fresh array for every number you test.

Try writing it yourself first!
Solved by 10 students in Fun Program Battle

Topic 4 · 8 problems

Primes & Divisors

Prime checks, prime factors and divisors.

40 Primes in a Range Easy prime check nested loops

Write a program that takes two integers, start and end, and counts the prime numbers in the range [start, end]. For example, between 10 and 20 there are 4 primes (11, 13, 17 and 19), so the output is 4.

Sample input
10
20
Sample output
Count of primes = 4

Tip: To test whether n is prime you only need to try divisors up to the square root of n (i * i <= n). Any larger factor would pair with a smaller one you have already tried.

Try writing it yourself first!
Solved by 54 students in Fun Program Battle
41 Prime Product Medium prime check long product

Write a program to read an integer n, print all prime numbers less than or equal to n in ascending order separated by commas, and also print the product of those primes. For example, for n = 10 the primes are 2, 3, 5, 7 and their product is 2 x 3 x 5 x 7 = 210.

Sample input
10
Sample output
2, 3, 5, 7
Product = 210

Tip: Products grow quickly, so store the product in a long.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
42 Prime Sum Easy prime check sum

Write a program to find the sum of all prime numbers less than a given number N. For example, for N = 10 the primes below 10 are 2, 3, 5 and 7, so the output is 2 + 3 + 5 + 7 = 17.

Sample input
10
Sample output
Sum of primes = 17

Tip: Read the question carefully: "less than N" means the loop condition is k < N, not k <= N.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
43 Prime Digit Sum Easy digit sum prime check

Write a program that checks whether the sum of the digits of a given number is a prime number. For example, for 23 the digit sum is 2 + 3 = 5, which is prime. For 123 the digit sum is 6 and for 37 it is 10, and neither is prime.

Sample input
23
Sample output
Sum of digits = 5
The digit sum is prime

Tip: Break the problem into two small jobs you already know, a digit sum and a prime check, and do them one after the other.

Try writing it yourself first!
Solved by 4 students in Fun Program Battle
44 Perfect Number Easy divisors sum

A perfect number is a positive integer equal to the sum of its proper divisors (all divisors except the number itself). For example, the proper divisors of 6 are 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is perfect. Write a program that checks whether a given number is perfect.

Sample input
6
Sample output
6 is a perfect number

Tip: No proper divisor can be bigger than n / 2, so the loop can stop there. 1 is not a perfect number, because it has no proper divisors.

Try writing it yourself first!
Solved by 8 students in Fun Program Battle
45 Prime Factor Sum Medium prime factorisation while loop

Write a program to find the sum of the distinct prime factors of a given number N. For example, 10 = 2 x 5, so the output is 2 + 5 = 7. 12 = 2 x 2 x 3, so the distinct prime factors are 2 and 3 and the output is 5. If N is less than 2, print a message.

Sample input
10
Sample output
Prime factors: 2 5
Sum = 7

Tip: Dividing out each factor completely (while n % i == 0, n = n / i) guarantees that every i which divides n is a prime. Whatever is left above 1 at the end is also a prime factor.

Try writing it yourself first!
Solved by 10 students in Fun Program Battle
46 K Divisors Medium counting divisors nested loops

Write a program that takes two integers N and K and counts the numbers less than N that have exactly K divisors. For example, for N = 10 and K = 2 the numbers 2, 3, 5 and 7 each have exactly 2 divisors, so the output is 4.

Sample input
10
2
Sample output
Numbers: 2 3 5 7
Count = 4

Tip: Numbers with exactly 2 divisors are the primes. Numbers with exactly 3 divisors are squares of primes (4, 9, 25, ...). Try different values of K and look for the pattern.

Try writing it yourself first!
Solved by 6 students in Fun Program Battle
47 Divisor Digit Sum Medium divisors digit sum

Write a program that takes an integer, finds all of its divisors, and adds up the digit sums of those divisors. For example, the divisors of 12 are 1, 2, 3, 4, 6 and 12. Their digit sums are 1, 2, 3, 4, 6 and 1 + 2 = 3, so the total is 19.

Sample input
12
Sample output
Divisors: 1 2 3 4 6 12
Total = 19

Tip: Each divisor must be copied into a temporary variable before you extract its digits. Changing the loop variable itself would break the loop.

Try writing it yourself first!
Solved by 4 students in Fun Program Battle

Topic 5 · 14 problems

Arrays

Search, count, swap, sort and rotate a list of values.

48 Swap Pairs Easy arrays swapping

Write a program to read an array of integers and swap each pair of neighbouring elements: the element at index 0 with index 1, index 2 with index 3, and so on. If the array has an odd length, the last element stays where it is. For example, [1, 2, 3, 4, 5] becomes [2, 1, 4, 3, 5].

Sample input
5
1 2 3 4 5
Sample output
2 1 4 3 5

Tip: Swapping always needs a third variable: temp = a[i]; a[i] = a[i+1]; a[i+1] = temp. Stepping i by 2 visits each pair once.

Try writing it yourself first!
Solved by 11 students in Fun Program Battle
49 Alternate Sum Easy arrays index parity

Write a program that reads a list of integers and calculates (sum of elements at even indices) - (sum of elements at odd indices). Indices start at 0. For example, for [1, 2, 3, 4, 5] the output is (1 + 3 + 5) - (2 + 4) = 3. An empty list gives 0.

Sample input
5
1 2 3 4 5
Sample output
Result = 3

Tip: The index decides the group, not the value. Check i % 2, not a[i] % 2.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
50 Peak Element Easy arrays neighbours boundary checks

A peak element is an element that is not smaller than its neighbours. Write a program to find the index of the first peak element in an array. The array has at least one element. For example, in [1, 3, 20, 4, 1, 0] the output is 2, because 20 is the peak.

Sample input
6
1 3 20 4 1 0
Sample output
Peak element 20 at index 2

Tip: The first and last elements have only one neighbour each. Check i > 0 before looking at a[i - 1], and i < n - 1 before looking at a[i + 1].

Try writing it yourself first!
Solved by 8 students in Fun Program Battle
51 Pair Sum Easy arrays nested loops

Write a program to read an array of integers and a target sum, then print every pair of values that adds up to the target. For example, for the array [1, 2, 3, 4, 5] and target 7 the output is (2, 5) and (3, 4). If there is no such pair, print a message.

Sample input
5
1 2 3 4 5
7
Sample output
(2, 5)
(3, 4)

Tip: Starting the inner loop at j = i + 1 checks each pair only once and never pairs an element with itself.

Try writing it yourself first!
Solved by 4 students in Fun Program Battle
52 Element Frequency Medium arrays bubble sort counting

Write a program to read N integers into an array and print how many times each different value occurs, in ascending order of value. The input does not have to be sorted. For example, for [4, 2, 2, 3, 4, 4, 1] the output is 1: 1, 2: 2, 3: 1, 4: 3.

Sample input
7
4 2 2 3 4 4 1
Sample output
1: 1
2: 2
3: 1
4: 3

Tip: After sorting, equal values sit next to each other, so you can count each group in one pass. This program uses bubble sort, the sorting method taught in ICSE.

Try writing it yourself first!
Solved by 3 students in Fun Program Battle
53 Remove Duplicates Medium arrays search building a new array

Write a program that reads an array of integers and builds a new array containing only the first occurrence of each value, keeping the original order. Do not use any built-in function for removing duplicates. For example, [1, 2, 2, 3, 4, 1, 5] becomes [1, 2, 3, 4, 5].

Sample input
7
1 2 2 3 4 1 5
Sample output
1 2 3 4 5

Tip: Keep a counter m for how many values are in the result so far. The result array can be the same size as the input, because you only use its first m slots.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
54 Evens First Easy arrays two passes

Write a program that reads an array and rearranges it so that all even numbers come before all odd numbers, keeping the original order within each group. For example, [3, 1, 2, 4, 5] becomes [2, 4, 3, 1, 5].

Sample input
5
3 1 2 4 5
Sample output
2 4 3 1 5

Tip: Two simple passes over the array keep the order stable. The first pass copies the evens and the second copies the odds.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
55 Longest Run Easy arrays counting runs maximum

Write a program that reads an array of integers and finds the length of the longest run of identical elements next to each other. For example, in [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] the number 4 appears four times in a row, so the output is 4.

Sample input
10
1 2 2 3 3 3 4 4 4 4
Sample output
Longest run = 4

Tip: This combines two common patterns: counting the current run and keeping track of the maximum so far.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
56 Reverse Array Easy arrays swapping palindrome

Write a program that reads an array of integers and reverses it in place (without a second array for the result). Then check whether the reversed array is the same as the original. If it is, print "Palindrome Array", otherwise print the reversed array.

Sample input
4
1 2 3 4
Sample output
Reversed array: 4 3 2 1

Tip: To reverse in place, swap the first and last elements, then the second and second-last, and so on. Stop at the middle (i < n / 2), or you will swap everything back.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
57 Pivot Index Medium arrays running sum

Write a program to find the pivot index of an array: the index where the sum of the elements to its left equals the sum of the elements to its right. Print -1 if no such index exists. For example, in [1, 2, 3, 2, 1] the output is 2, because 1 + 2 = 2 + 1.

Sample input
5
1 2 3 2 1
Sample output
Pivot index = 2

Tip: Instead of re-adding both sides for every index, compute the total once. Then right = total - left - a[i], which is much faster.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
58 Peaks & Valleys Easy arrays neighbours

Write a program that reads an array of integers and counts its peaks and valleys. A peak is an element greater than both neighbours, and a valley is an element smaller than both neighbours. The first and last elements are never counted. For example, in [1, 3, 2, 4, 1] the peaks are 3 and 4 and the valley is 2, so the total is 3. An array with fewer than 3 elements has none.

Sample input
5
1 3 2 4 1
Sample output
Peaks = 2
Valleys = 1
Total = 3

Tip: Loop only from i = 1 to n - 2, so that both a[i - 1] and a[i + 1] always exist.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
59 Array Median Medium arrays bubble sort odd/even length

Write a program that reads an array of integers and prints its median: the middle value after sorting. If the array has an even number of elements, the median is the average of the two middle values. For example, the median of [1, 3, 2, 4, 5] is 3, and the median of [1, 2, 3, 4] is 2.5. Print a message if the array is empty.

Sample input
5
1 3 2 4 5
Sample output
Median = 3.0

Tip: Divide by 2.0, not 2, when averaging. Integer division would turn 2.5 into 2.

Try writing it yourself first!
Solved by 5 students in Fun Program Battle
60 Rotation Check Medium arrays modulus (%) rotation

Write a program that reads two arrays of the same length and checks whether the second is a rotation of the first. For example, [3, 4, 5, 1, 2] is a rotation of [1, 2, 3, 4, 5] (shifted by 2 places), but [1, 3, 2] is not a rotation of [1, 2, 3].

Sample input
5
1 2 3 4 5
3 4 5 1 2
Sample output
The second array is a rotation of the first

Tip: The index (i + shift) % n wraps around to the start of the array. This is how you treat an array as a circle.

Try writing it yourself first!
Solved by 9 students in Fun Program Battle
61 Missing Number Easy arrays consecutive values

Write a program that reads a sorted list of consecutive integers with exactly one number missing from the middle, and prints the missing number. For example, for [1, 2, 3, 5, 6] the output is 4.

Sample input
5
1 2 3 5 6
Sample output
Missing number = 4

Tip: In a consecutive list each element is exactly one more than the one before it. The first place this fails shows you the gap.

Try writing it yourself first!
Solved by 6 students in Fun Program Battle

Topic 6 · 10 problems

Strings

Vowels, palindromes, anagrams and word tricks.

62 Vowel Counter Easy strings charAt case-insensitive

Write a program that reads a line of text and counts its vowels (a, e, i, o, u), treating uppercase and lowercase the same. Also list which different vowels appeared. If there are no vowels, say so. For example, "Programming is Fun" has 5 vowels, and the vowels found are a, i, o, u.

Sample input
Programming is Fun
Sample output
Total vowels = 5
Vowels found: a i o u

Tip: Convert the text to lower case first so you only need to check five characters. String.indexOf(ch) >= 0 is a quick way to test whether ch is a vowel.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
63 Capital Vowels Easy strings Character methods building strings

Write a program that reads a single word, counts its vowels, and builds a new word in which every vowel is changed to upper case. For example, for "hello" the output is a vowel count of 2 and the modified word "hEllO".

Sample input
hello
Sample output
Vowels = 2
Modified word = hEllO

Tip: Strings in Java cannot be changed. Build a new String by adding one character at a time.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
64 Repeat Characters Easy strings nested loops

Write a program that takes a string and an integer k, and prints the string with each character repeated k times. For example, for "abc" and 2 the output is "aabbcc". Upper and lower case are kept as they are.

Sample input
abc
2
Sample output
aabbcc

Tip: Reading a whole line with nextLine() and then a number with nextInt() works in this order. The reverse order (nextInt() then nextLine()) needs an extra nextLine() to skip the leftover newline.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
65 Palindrome Sentence Medium strings Character.isLetterOrDigit two pointers

Write a program that reads a string and checks whether it is a palindrome, ignoring spaces, punctuation and the difference between upper and lower case. For example, "Madam, I'm Adam" is a palindrome, but "Hello" is not.

Sample input
Madam, I'm Adam
Sample output
Palindrome

Tip: First "clean" the text by keeping only letters and digits in lower case, then compare it with its reverse. Cleaning first makes the check itself very simple.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
66 Reverse Words Medium strings split arrays of strings

Write a program that reads a sentence and prints its words in reverse order, keeping each word itself unchanged. Extra spaces between words, and spaces at the start or end, should be ignored. For example, "Hello World from Java" becomes "Java from World Hello".

Sample input
Hello World from Java
Sample output
Java from World Hello

Tip: trim() removes the outer spaces, and split("\\s+") splits on one or more spaces, so repeated spaces never produce empty words.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
67 Chunk Reverse Medium strings substring loops with steps

Write a program that reads a string and an integer n, splits the string into chunks of n characters, reverses each chunk, and joins them back together. The last chunk may be shorter. For example, "abcdef" with n = 2 becomes "badcfe".

Sample input
abcdef
2
Sample output
badcfe

Tip: Math.min(start + n, length) stops the last chunk from going past the end of the string.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
68 Remove Repeats Easy strings comparing neighbours

Write a program that reads a string and removes consecutive duplicate characters, so that a run of the same character becomes a single character. Case and special characters are kept. For example, "Hellooo World!!" becomes "Helo World!".

Sample input
Hellooo World!!
Sample output
Helo World!

Tip: Compare each character with the one just before it, and keep it only if it is different.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
69 Unique Letters Medium strings counting array two passes

Write a program that reads a string and finds the letters that appear exactly once, ignoring case and anything that is not a letter. Print those letters in the order they appear, and their count. If there are none, print "No unique characters found". For example, in "Hello, World!" the letters h, e, w, r and d appear once, so the count is 5.

Sample input
Hello, World!
Sample output
Unique letters: h e w r d
Count = 5

Tip: An int[26] array counts every letter, using ch - 'a' as the index. A second pass over the string then prints the letters whose count is 1, in their original order.

Try writing it yourself first!
Solved by 2 students in Fun Program Battle
70 Anagram Checker Medium strings counting array

Two strings are anagrams if they contain exactly the same letters the same number of times, just in a different order. Write a program that reads two strings and checks whether they are anagrams, ignoring spaces and case. For example, "Listen" and "Silent" are anagrams, and so are "Dormitory" and "Dirty room".

Sample input
Listen
Silent
Sample output
Anagrams

Tip: Instead of sorting, count the letters: add 1 for every letter in the first string and subtract 1 for every letter in the second. They are anagrams only if every count ends at 0.

Try writing it yourself first!
Solved by 6 students in Fun Program Battle
71 Word Chain Easy strings charAt concatenation

Write a program that reads two words and checks whether the last character of the first word is the same as the first character of the second word. If it is, print the two words joined together. Otherwise print a message saying they cannot be chained. For example, "apple" and "eagle" can be chained to give "appleeagle".

Sample input
apple
eagle
Sample output
Chained: appleeagle

Tip: The last character of a string s is s.charAt(s.length() - 1). Forgetting the "- 1" is one of the most common String errors.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle

Topic 7 · 4 problems

Matrices

2-D arrays: diagonals, boundaries and mirror images.

72 Diagonal Sums Easy 2-D arrays diagonals

Write a program that reads a square matrix of size n x n and prints the sum of its main diagonal (top-left to bottom-right) and the sum of its secondary diagonal (top-right to bottom-left). For example, for the matrix 1 2 3 / 4 5 6 / 7 8 9 both sums are 15 (1 + 5 + 9 and 3 + 5 + 7).

Sample input
3
1 2 3
4 5 6
7 8 9
Sample output
Main diagonal sum = 15
Secondary diagonal sum = 15

Tip: On the main diagonal the row equals the column (a[i][i]). On the secondary diagonal the column is n - 1 - i. One loop handles both.

Try writing it yourself first!
Solved by 5 students in Fun Program Battle
73 Boundary Sum Easy 2-D arrays boundary conditions

Write a program that reads the number of rows and columns of a matrix, then its elements, and prints the sum of the elements on its boundary: the first row, the last row, the first column and the last column. It must also work when the matrix has only one row or one column. For example, for 1 2 3 / 4 5 6 / 7 8 9 the boundary sum is 40 (everything except the 5 in the middle).

Sample input
3 3
1 2 3
4 5 6
7 8 9
Sample output
Boundary sum = 40

Tip: Checking each cell with one condition (i == 0 || i == r - 1 || j == 0 || j == c - 1) never counts a corner twice. Adding the four edges separately would.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
74 Diagonal Matrix Easy 2-D arrays flag variable

A diagonal matrix is a square matrix in which every element outside the main diagonal is zero. Write a program that reads the size n and an n x n matrix, and prints "Yes" if it is a diagonal matrix, otherwise "No".

Sample input
3
1 0 0
0 5 0
0 0 9
Sample output
Yes

Tip: To prove a property for every element you must check them all. To disprove it, a single counter-example is enough, so you can stop at the first non-zero element off the diagonal.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle
75 Mirror Matrix Easy 2-D arrays reversing rows

Write a program that reads a matrix with r rows and c columns and prints its mirror image across the vertical axis, so that each row appears reversed. For example, the matrix 1 2 3 / 4 5 6 becomes 3 2 1 / 6 5 4.

Sample input
2 3
1 2 3
4 5 6
Sample output
3 2 1
6 5 4

Tip: Column j of the mirror image is column c - 1 - j of the original. You can print the mirror directly without creating a second matrix.

Try writing it yourself first!
Solved by 1 student in Fun Program Battle

Fun Program Battle

Ready for a real battle?

Get a fresh problem at your level, write your program in Java or Python, and have it marked instantly with a corrected version and feedback.

Start Free