TheBoringGuy
TheBoringGuy

Reputation: 151

Multi of 3 If else statement

How do I do it if I enter a number that is a multiple of 3 it display the if statement for a number that isn't multi of 3 it goes to the else statement. I would be very thankful if you could help me.

        Console.Write("Enter Number: ");
        int N = Convert.ToInt32(Console.ReadLine());
        if (N == 3) //<<< what do I do here
        {
            Console.WriteLine("Is multi of 3");
        }
        else
        {
            Console.WriteLine("Is not multi of 3");
        }
        Console.ReadLine();

Upvotes: 0

Views: 144

Answers (2)

user1968030
user1968030

Reputation:

This is simple:

 if (N % 3 == 0)
     {
      Console.WriteLine("Is multi of 3");
     }

So Use:

 Console.Write("Enter Number: ");
 int N = Convert.ToInt32(Console.ReadLine());
 if (N % 3 == 0)
 {
  Console.WriteLine("Is multi of 3");
 }
 else
 {
    Console.WriteLine("Is not multi of 3");
 }
  Console.ReadLine();

Upvotes: 3

Grant Winney
Grant Winney

Reputation: 66499

Use the modulus operator instead:

if (N % 3 == 0)

It returns the remainder after doing a division.

If the remainder of dividing by 3 is 0, you know you have a multiple of 3.

Upvotes: 6

Related Questions