How to resolve the algorithm Even or odd step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Even or odd step by step in the C# programming language

Table of Contents

Problem Statement

Test whether an integer is even or odd. There is more than one way to solve this task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Even or odd step by step in the C# programming language

The provided C# code implements various methods to determine whether an integer is even or odd.

Within the EvenOrOdd class, there are six methods:

  1. IsEvenBitwise: Checks if the integer is even using bitwise operation.
  2. IsOddBitwise: Checks if the integer is odd using bitwise operation.
  3. IsEvenRemainder: Calculates the remainder when dividing the integer by 2 and checks if it's 0.
  4. IsOddRemainder: Calculates the remainder when dividing the integer by 2 and checks if it's not 0.
  5. IsEvenModulo: Checks if the remainder of dividing the integer by 2 is 0 using the modulo operator.
  6. IsOddModulo: Checks if the remainder of dividing the integer by 2 is not 0 using the modulo operator.

In the Program class, the Main method:

  1. Sets an integer variable num to 26 (or any other integer value).
  2. Uses the IsEvenBitwise method (or any other even/odd checking method) to determine if num is even.
  3. Prints "Even" if num is even, and "Odd" if it's odd.
  4. Repeats this process using the IsOddBitwise method (or any other even/odd checking method).

In the specific example provided, since 26 is an even number, both IsEvenBitwise and IsOddModulo will return true, resulting in the program printing "Even" twice.

Source code in the csharp programming language

namespace RosettaCode
{
    using System;

    public static class EvenOrOdd
    {
        public static bool IsEvenBitwise(this int number)
        {
            return (number & 1) == 0;
        }

        public static bool IsOddBitwise(this int number)
        {
            return (number & 1) != 0;
        }

        public static bool IsEvenRemainder(this int number)
        {
            int remainder;
            Math.DivRem(number, 2, out remainder);
            return remainder == 0;
        }

        public static bool IsOddRemainder(this int number)
        {
            int remainder;
            Math.DivRem(number, 2, out remainder);
            return remainder != 0;
        }

        public static bool IsEvenModulo(this int number)
        {
            return (number % 2) == 0;
        }

        public static bool IsOddModulo(this int number)
        {
            return (number % 2) != 0;
        }
    }
    public class Program
    {
        public static void Main()
        {
            int num = 26;               //Set this to any integer.
            if (num.IsEvenBitwise())    //Replace this with any even function.
            {
                Console.Write("Even");
            }
            else
            {
                Console.Write("Odd");
            }
            //Prints "Even".
            if (num.IsOddBitwise())    //Replace this with any odd function.
            {
                Console.Write("Odd");
            }
            else
            {
                Console.Write("Even");
            }
            //Prints "Even".
        }
    }
}


  

You may also check:How to resolve the algorithm Topological sort step by step in the Crystal programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Empty program step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Entropy step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Get system command output step by step in the Nim programming language