Check if a number is divisible by 3

后端 未结 16 2926
予麋鹿
予麋鹿 2020-12-01 01:30

I need to find whether a number is divisible by 3 without using %, / or *. The hint given was to use atoi() function. Any

16条回答
  •  感动是毒
    2020-12-01 02:09

    C# Solution for checking if a number is divisible by 3

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int num = 33;
                bool flag = false;
    
                while (true)
                {
                    num = num - 7;
                    if (num == 0)
                    {
                        flag = true;
                        break;
                    }
                    else if (num < 0)
                    {
                        break;
                    }
                    else
                    {
                        flag = false;
                    }
                }
    
                if (flag)
                    Console.WriteLine("Divisible by 3");
                else
                    Console.WriteLine("Not Divisible by 3");
    
                Console.ReadLine();
    
            }
        }
    }
    

提交回复
热议问题