Reputation: 151
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
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
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