I actually wrote this contest and this is the code I used to be marked. I've since fixed up the code and rewrote it in Java. Here is a link to that fixed up code.
You know a family with three children. Their ages form an arithmetic sequence: the difference in ages between the middle child and youngest child is the same as the difference in ages between the oldest child and the middle child. For example, their ages could be 5, 10 and 15, since both adjacent pairs have a difference of 5 years.
Given the ages of the youngest and middle children, what is the age of the oldest child?
Input Specification
The input consists of two integers, each on a separate line. The first line is the age Y of the
youngest child (0 ≤ Y ≤ 50). The second line is the age M of the middle child (Y ≤ M ≤ 50).
Output Specification
The output will be the age of the oldest child.
Sample Input 1
12
15
Output for Sample Input 1
18
Sample Input 2
10
10
Output for Sample Input 2
10
Option Strict On Public Class Form1 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim Y As Integer Dim M As Integer Try Y = CInt(txtYoungest.Text) M = CInt(txtMiddle.Text) If Y > M Or M > 50 Then MsgBox("Please make sure the middle age is greater than or equal to the youngest age and their ages are no greater than 50.") txtYoungest.Text = "" txtMiddle.Text = "" Else Dim d As Integer, O As Integer d = M - Y O = M + d txtOldest.Text = CStr(O) End If Catch ex As Exception MsgBox("Please enter integer numbers.") txtYoungest.Text = "" txtMiddle.Text = "" End Try End Sub Private Sub txtYoungest_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtYoungest.TextChanged If txtYoungest.Text.StartsWith(".") Then txtYoungest.Text = "" MsgBox("Please enter integer numbers.") End If End Sub Private Sub txtMiddle_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMiddle.TextChanged If txtMiddle.Text.StartsWith(".") Then txtMiddle.Text = "" MsgBox("Please enter integer numbers.") End If End Sub End Class
Using their test cases:
50 50
And the output to that is:
50
And that is exactly the expected output
DOWNLOAD as .in0 50
And the output to that is:
100
And that is exactly the expected output
DOWNLOAD as .in21 37
And the output to that is:
53
And that is exactly the expected output
DOWNLOAD as .in2 9
And the output to that is:
16
And that is exactly the expected output
DOWNLOAD as .in11 22
And the output to that is:
33
And that is exactly the expected output
DOWNLOAD as .in
Created: February 26, 2013
Completed in full by: Michael Yaworski