0

I'm trying to implement the Euclidean Algorithm with user input in a C# snippet as part of my process of learning this language. MVS tells me that there is an error with the if and elif statements and with the end braces of these statements. Now, coming from a pythonic background this seems pretty natural to me, so please help me identify the possible mistakes. Help is much appreciated.

Code:

namespace EuclideanAlgorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter two numbers to calculate their GCD"); 

            int input1 = Convert.ToInt32(Console.ReadLine());
            int input2 = Convert.ToInt32(Console.ReadLine());
            int remainder;
            int a;
            int b;

            if (input1 == input2);
            {
                Console.Write("The GCD of", input1, "and", input2, "is", input1);
                Console.ReadLine();
            }
            else if (input1 > input2);
            {
                a = input1;
                b = input2;

                while (remainder != 0);
                {
                    remainder = a % b;
                    a = b;
                    b = remainder;
                }
                Console.Write("The GCD of", input1, "and", input2, "is", b);
                Console.ReadLine();
            }
            else if (input1 < input2);
            {
                a = input2;
                b = input1;

                while (remainder != 0);
                {
                    remainder = a % b;
                    a = b;
                    b = remainder;
                }

                Console.WriteLine("The GCD of", input1, "and", input2, "is", b);
                Console.ReadLine();
            }
        }
    }
}

3 Answers 3

7

Well you need to remove the semicolons on the ifs. So:

if (input1 == input2);

becomes:

if (input1 == input2)

This also goes for the else if and the while. Also just a side note:

Console.Write("The GCD of", input1, "and", input2, "is", input1);

This will produce:

The GCD of

If you want to do a string.Format you need to do it like this:

Console.Write("The GCD of {0} and {1} is {2}", input1, input2, input1);

Here is more info on string.Format

1 more thing - make sure that you initialize your remainder where you're setting it up, otherwise you won't be able to compile Local variable remainder might not be initialized before accessing:

int remainder = 0;

I hope this helps.

EDIT

If you want your remainder to be anything other than a 0 the first time you evaluate it you can instead use a do/while loop:

do
{
    remainder = a % b;
    a = b;
    b = remainder;

} while (remainder != 0);
Sign up to request clarification or add additional context in comments.

5 Comments

This was really helpful... THANK YOU!!! On a sidenote I'd like to know why does the while statement does not end as soon as the remainder is assigned the value of 0 but after the loop is finished. Thanks in advance.
@SebastianGarrido Because you need to calculate it before you evaluate it, I hope I'm making sense. Basically calculate it once more before the while -> remainder = a % b; and let it go to the while.
Is there any way to bypass this behaviour?
@SebastianGarrido Well, if you remainder is initialized as a 0 you can't get into the while loop, so you need to calculate it before hand, OR you can do it in a do/while (the do will run always at least once before the evaluation of the loop), check my edit
@SebastianGarrido: Think of while(x) s; for expression x and statement s as being just a pleasant way to write START: if (!x) goto END; s; goto START; END: ; because that's what it is.
0

You have semi colons on those if statements

if (input1 == input2);

else if (input1 < input2);

When there are semi colons on there it doesn't enter the brackets, change them to

if (input1 == input2)

else if (input1 < input2)

Since you already have yout { there we don't need to add them again,

Now it should work

Same for your while loop at the top, which I just saw

1 Comment

I was wondering why the semi colons where in use, now thanks to you I have some intuition, thanks.
0

The following lines are wrong:

if (input1 == input2);
[...]
else if (input1 > input2);
[...]
while (remainder != 0);
[...]
else if (input1 < input2);
[...]
while (remainder != 0);

The semicolon (;) at the end of each of these ends the statement, making the braces ({) after it incorrect.

Do NOT end if, while, and for statements with semicolons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.