Problem 2 in C and Java.