# [RESOLVED] C# do-while? Necklace Problem

• October 10th, 2011, 03:18 PM
Nin9tySe7en
[RESOLVED] C# do-while? 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-column-digit.This process is repeated until the “necklace” closes by returning to the original two numbers. For example, if the starting 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
I need a program that will ask for the user two input 2 numbers ( firstNumber and secondNumber)
Then the program will show the number of steps until the last 2 numbers are the same.
For example:

Enter the first number: 1
Enter the second number: 8
1 8 9 7 6 3 9 2 1 3 4 7 1 8

This is what I have created...

static void Main(string[] args)
{
double firstNumber;
double secondNumber;

Console.Write("Enter the first number:");

Console.Write("\nEnter the second number:");

Console.WriteLine(firstNumber + secondNumber);

do
{
}

}
}
}

Im not sure if it works at the start, but I do know that it doesnt work completely because it goes into an "infinity" loop.

• October 10th, 2011, 04:23 PM
DataMiser
Re: C# do-while? Necklace Problem
You're not splitting off the ones column
• October 10th, 2011, 04:40 PM
Nin9tySe7en
Re: C# do-while? Necklace Problem
Oh your right. How exactly would I do that ?
• October 10th, 2011, 05:32 PM
you69
Re: C# do-while? Necklace Problem
If you have questions don't hesitate to ask :).

Code:

class Program
{
static void Main(string[] args)
{
int original_a = 0;
int original_b = 0;

try
{
Console.Write("Enter the first number: ");
if (original_a < 0 || original_a >= 10) throw new Exception();

Console.Write("Enter the second number: ");

int a = original_a;
int b = original_b;

int steps = 0;

do
{
if (steps == 0) Console.Write(a + " " + b);

if (steps % 2 == 0)
{
a += b;
if (a >= 10) a %= 10;

Console.Write(" " + a);
}
else
{
b += a;
if (b >= 10) b %= 10;

Console.Write(" " + b);
}

steps++;
} while (a != original_a || b != original_b);

Console.WriteLine("");
Console.WriteLine("Your number required " + steps + " steps.");
}
catch (FormatException e)
{
Console.WriteLine("Error: You have not entered a valid number.");
}
catch (Exception e)
{
Console.WriteLine("Error: You have not entered number between 0 to 9.");
}
}
}

• October 10th, 2011, 05:46 PM
Nin9tySe7en
Re: C# do-while? Necklace Problem
Quote:

Originally Posted by you69
Code:

class Program
{
static void Main(string[] args)
{
//declares variables equal to 0 ? Why are they equal to 0 ? Shouldn't they be equal to whatever the user inputs?
int original_a = 0;
int original_b = 0;

//what is "try" ?
try
{
Console.Write("Enter the first number: ");
//What does this statement do? Especially the "throw new exception()" part
if (original_a < 0 || original_a >= 10) throw new Exception();

Console.Write("Enter the second number: ");

int a = original_a;
int b = original_b;

//why 0 ? Again...
int steps = 0;

do
{

//What goes inside the " " ? Furthermore, What exactly is this statement doing ?
if (steps == 0) Console.Write(a + " " + b);

//why do we want to know if it's divisible by 2 ?
if (steps % 2 == 0)
{

//a += b ???
a += b;
if (a >= 10) a %= 10;

//whats with the empty " " again ?
Console.Write(" " + a);
}
else
{

//b += a ???
b += a;
if (b >= 10) b %= 10;

Console.Write(" " + b);
}

steps++;
} while (a != original_a || b != original_b);

Console.WriteLine("");
Console.WriteLine("Your number required " + steps + " steps.");
}

//catch ?  (FormatException e) ?
catch (FormatException e)
{
Console.WriteLine("Error: You have not entered a valid number.");
}
//Exception e ?
catch (Exception e)
{
Console.WriteLine("Error: You have not entered number between 0 to 9.");
}
}
}

You said don't hesitate to ask questions... So I didn't. xD
BTW. THANKS SO MUCH
• October 10th, 2011, 06:09 PM
you69
Re: C# do-while? Necklace Problem
Code:

int original_a = 0;
int original_b = 0;

that's what called to initialize a variable. you should to do that otherwise usually the variable will contain garbage. also it is a good idea to know which value the variable have in the begginig to avoid surprises.

Code:

try
{

briefly explaining, when there is some kind of exception (in this program the exceptions mean invalid input, but it's can be anything) between the 'try' block it's throw an exception. what it's mean? it's mean that the program stops and goes to the suitable 'catch' block (if exists).

Code:

if (original_a < 0 || original_a >= 10) throw new Exception();
this is if condition that tets if the original number is less than 0 or larger than 10 (because the input number should be between 0 to 9) and if it is it's throw an exception.

Code:

int steps = 0;
initializing variable again (as i explained before).

Code:

if (steps == 0) Console.Write(a + " " + b);
it's mean that we write in one line what we have in variable 'a' then white space and then what we have in variable 'b'.

Code:

if (steps % 2 == 0)
here we check if the number of steps is even or odd; mod 2 of any number will return 0 if it's a even number or the remainde if it's a odd number. if it's even we should change 'a' variable if it's odd we should change 'b' variable.

Code:

a += b;
b += a;

in other words, it's like to say:

Code:

a = a + b;
b = b + a;

• October 10th, 2011, 06:36 PM
Nin9tySe7en
Re: C# do-while? Necklace Problem
Okay thanks :D. And one last question- What does " if (a >= 10) a &#37;= 10; " do ?
• October 10th, 2011, 06:53 PM
you69
Re: C# do-while? Necklace Problem
Code:

if (a >= 10) a %= 10;
it's means that if the new number (a + b) is larger than 10 we take only the right digit (Modulo 10).
for example (assuming that the numbers are integers):
14 / 10 = 1.
14 % 10 = 4 ('4' is the remainder of 14 / 10).
• October 10th, 2011, 07:05 PM
Nin9tySe7en
Re: C# do-while? Necklace Problem
Quote:

Originally Posted by you69
assuming that the numbers are integers

this brings up another point. How would I make sure that the person is to type in a single-digit number/integer. There is nothing stopping them from typing in a decimal ?
• October 10th, 2011, 07:36 PM
QuinnJohns
Re: C# do-while? Necklace Problem
Quote:

Originally Posted by Nin9tySe7en
this brings up another point. How would I make sure that the person is to type in a single-digit number/integer. There is nothing stopping them from typing in a decimal ?

Read up on Console.Read() here. Essentially, it reads the next character from the standard input stream. As opposed to Console.ReadLine(), which reads the "next line of characters" from the standard input stream.

There are other ways of handling input, but in your case with digits 0-9, I'd suggest using Read().

Unless, you are wanting to accept decimals, etc.
• October 10th, 2011, 07:52 PM
Nin9tySe7en
Re: C# do-while? Necklace Problem
Final Question.... most likely. xD
I was studying the coding trying to make sense of everything so what I did was I re-wrote it all in my own style. I have come to understand everything except for the exceptions

why did the does it need to be written like:

catch (FormatException e)
{
}

couldn't it be written like:

catch (FormatException)
{
}

Why is the "e" there ?
• October 10th, 2011, 08:12 PM
TheGreatCthulhu
Re: C# do-while? Necklace Problem
Because you usually don't catch exceptions just so that you can pretend they never happened, but to handle them instead. Handling might involve correcting some variables (maybe the input was invalid) and resuming with the appropriate value if possible, or notifying the user about the error, and providing the appropriate info (because no user likes to see the app crash and throw a stack trace at him).
When doing this, you might want to show the exception message, or check some properties of a specific exception, and the 'e' enables you to access those. The 'e' is just a variable name that stores an exception object of the given type.
Exceptions are just objects like any other.
• October 10th, 2011, 08:20 PM
TheGreatCthulhu
Re: C# do-while? Necklace Problem
Quote:

Originally Posted by Nin9tySe7en
Okay thanks :D. And one last question- What does " if (a >= 10) a %= 10; " do ?

P.S. About that: don't be confused it's all written in one line - that can be re-written as:
Code:

if (a >= 10)
a %= 10;

It's just a standard conditional, with single-statement body.

It's usually written in that form - indentation, blank spaces and line breaks are just a matter of readability and coding style; C# doesn't care much about them. That is usually written in two lines, second line indented, as it's more readable and easier to understand, but when the body of the if-statement is so short, people sometimes do a one-liner.
• October 11th, 2011, 12:48 AM
QuinnJohns
Re: C# do-while? Necklace Problem
Quote:

Originally Posted by Nin9tySe7en
Final Question.... most likely. xD
I was studying the coding trying to make sense of everything so what I did was I re-wrote it all in my own style. I have come to understand everything except for the exceptions

why did the does it need to be written like:

catch (FormatException e)
{
}

couldn't it be written like:

catch (FormatException)
{
}

Why is the "e" there ?

The neat part about Exceptions, are they help you locate the problems. Often times you may want to generate an error message, to display to the user of your applications. You have various higher level exceptions such as FormatException, but also lower ones, such as Exception. Regardless, e, allows you to display exception information.

Code:

MessageBox.Show("Error", e.Message);