Answer to Exercise 7

The solution to this exercise starts by splitting the numbers string variable. This creates an array of all the individual numbers. You then loop round each number and check its value. The checking is done on two numbers at a time, the current array position and the next array position. In the code below, this is done with the variable called i and i + 1. This gets you the two numbers you need to check. Because we set up a string array, we need to convert these to integers. If the first number is greater than the second number then we use another IF Statement to check if those two numbers have a difference of 1. If so then we increment a counter variable. The ELSE part below is to deal with ascending numbers (1-2-3-4-5). If the counter variable has a value of 4 then we know that all five numbers are consecutive.

 

C# NET Answer

private void btnConsecutiveNumbers_Click(object sender, EventArgs e){	string numbers = "10-9-8-7-6";	//string numbers = "1-2-3-4-5";	string[] array_numbers = numbers.Split('-');	int counter = 0;	for (int i = 0; i < array_numbers.Length - 1; i++)	{	    int first_num = int.Parse(array_numbers[i]);	    int second_num = int.Parse(array_numbers[i + 1]);	    if (first_num > second_num)	    {	        if (first_num - second_num == 1)	        {				counter++;	        }	    }	    else	    {	        if (second_num - first_num == 1)	        {	            counter++;	        }	    }             	}	if (counter == 4)	{	    listBox1.Items.Add("consecutive numbers");	}	else	{	    listBox1.Items.Add(" not consecutive numbers");	}}

 

VB NET Answer

Private Sub btnConsecutiveNumbers_Click(sender As Object, e As EventArgs) Handles btnConsecutiveNumbers.Click	Dim numbers As String = "10-9-8-7-6"	'Dim numbers As String = "1-2-3-4-5"	Dim array_numbers() As String	array_numbers = numbers.Split("-")	Dim i As Integer	Dim counter As Integer = 0	For i = 0 To array_numbers.Length - 2	    Dim first_num = Integer.Parse(array_numbers(i))	    Dim second_num = Integer.Parse(array_numbers(i + 1))	    If first_num > second_num Then					If first_num - second_num = 1 Then		    	counter = counter + 1			End If	    Else			If second_num - first_num = 1 Then		    	counter = counter + 1			End If	    End If	Next	If counter = 4 Then	    ListBox1.Items.Add("consecutive numbers")	Else	    ListBox1.Items.Add("not consecutive numbers")	End IfEnd Sub

 

Java Answer

public static void main(String[] args) {	String numbers = "10-9-8-7-6";    //String numbers = "1-2-3-4-5";    String[] array_numbers = numbers.split("-");    int counter = 0;    for (int i = 0; i < array_numbers.length - 1; i++)    {        int first_num = Integer.parseInt(array_numbers[i]);        int second_num = Integer.parseInt(array_numbers[i + 1]);        if (first_num > second_num)        {            if (first_num - second_num == 1)            {                counter++;            }        }        else        {            if (second_num - first_num == 1)            {                counter++;            }        }    }    if (counter == 4)    {        System.out.println("consecutive numbers");    }    else    {        System.out.println("non consecutive numbers");    }}