Sebastian Garrido
Sebastian Garrido

Reputation: 311

error in C# with if statement

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();
            }
        }
    }
}

Upvotes: 0

Views: 292

Answers (3)

abelenky
abelenky

Reputation: 64740

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.

Upvotes: 0

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15158

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);

Upvotes: 7

Jim
Jim

Reputation: 482

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

Upvotes: 0

Related Questions