V
V
Victoria Kabishova2021-10-01 22:45:06
C++ / C#
Victoria Kabishova, 2021-10-01 22:45:06

How can I fix the code so that the % operation in the code is called no more than twice?

Hello, I need to fix the code in such a way that the % operation in the code is called no more than two times, I understand that for this you need to overload the % operator, but I don’t quite understand how to write it:

using System;

namespace Numbers5
{
    internal class Program
    {
        static void Main()
        {
            int i = 0;
            while (i++ <= 99)
            {
                if (i % 3 != 0 && i % 5 != 0)
                {
                    Console.WriteLine(i);
                }
                else if (i % 3 == 0 & i % 5 == 0)
                {
                    Console.WriteLine("OuuMay");
                }
                else
                {
                    if (i % 3 == 0)
                        Console.WriteLine("Ouu");
                    if (i % 5 == 0)
                        Console.WriteLine("May");
                }
            }
            Console.ReadKey();
        }
    }
}

Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-10-02
@Parsifal31017

Emmm. Just put them in variables?

using System;

for (var i = 1; i < 100; i++)
{
    var dividesBy3 = i % 3 == 0;
    var dividesBy5 = i % 5 == 0;

    if (dividesBy3 && dividesBy5)
        Console.WriteLine("OuuMay");
    else if (dividesBy3)
        Console.WriteLine("Ouu");
    else if (dividesBy5)
        Console.WriteLine("May");
    else
        Console.WriteLine(i);
}
Console.ReadKey();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question