An interesting problem in number theory is sometimes called the Necklace Problem. This problem begins with two single-digit numbers. The next number is obtained by adding the first two numbers together and saving only the ones digit. This process is repeated until the necklace closes by returning to the original two numbers. For example, if the starting two numbers are 1 and 8, twelve steps are required to close the necklace: 1 8 9 7 6 3 9 2 1 3 4 7 1 8.

Create a Necklace application that prompts the user for two single-digit integers and then displays the sequence and the number of steps taken.

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
32
33
34
35
36
37
38
39
using System;
 
namespace Necklace_Problem
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the First Number:");
            int Num1 = Convert.ToInt32(Console.ReadLine());
 
            Console.Write("Enter the Second Number:");
            int Num2 = Convert.ToInt32(Console.ReadLine());
 
            Console.WriteLine("Output = {0}", Necklace(Num1, Num2));
            Console.Read();
        }
 
        private static string Necklace(int OrigFirstNum, int OrigSecNum)
        {
            string Output = "";
 
            int FirstNum = OrigFirstNum;
            int SecondNum = OrigSecNum;
 
            Output = OrigFirstNum + " " + OrigSecNum + " ";
            do
            {
                int Result = FirstNum + SecondNum;
                int LastDigit = Result % 10;
                FirstNum = SecondNum;
                SecondNum = LastDigit;
                Output += SecondNum + " ";
            } while (!(FirstNum == OrigFirstNum && SecondNum == OrigSecNum));
 
            return Output;
        }
    }
}
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
32
Namespace Necklace_Problem
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Console.Write("Enter the First Number:")
            Dim Num1 As Integer = Convert.ToInt32(Console.ReadLine())
 
            Console.Write("Enter the Second Number:")
            Dim Num2 As Integer = Convert.ToInt32(Console.ReadLine())
 
            Console.WriteLine("Output = {0}", Necklace(Num1, Num2))
            Console.Read()
        End Sub
 
        Private Shared Function Necklace(ByVal OrigFirstNum As Integer, ByVal OrigSecNum As Integer) As String
            Dim Output As String = ""
 
            Dim FirstNum As Integer = OrigFirstNum
            Dim SecondNum As Integer = OrigSecNum
 
            Output = OrigFirstNum + " " + OrigSecNum + " "
            Do
                Dim Result As Integer = FirstNum + SecondNum
                Dim LastDigit As Integer = Result Mod 10
                FirstNum = SecondNum
                SecondNum = LastDigit
                Output += SecondNum + " "
            Loop While Not (FirstNum = OrigFirstNum AndAlso SecondNum = OrigSecNum)
 
            Return Output
        End Function
    End Class
End Namespace
No votes yet.
Please wait...