w3resource

C#: Check whether two given non-negative integers have the same last digit


Same Last Digit for Two Integers

Write a C# Sharp program to check whether two given non-negative integers have the same last digit.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Check if two given non-negative integers have the same last digit.

Sample Solution:-

C# Sharp Code:

using System;

// Namespace declaration
namespace exercises
{
    // Class declaration
    class Program
    {
        // Main method - entry point of the program
        static void Main(string[] args)
        {
            // Displaying the output of the 'test' method with different integer inputs
            Console.WriteLine(test(123, 456)); // Output: False
            Console.WriteLine(test(12, 512));  // Output: True
            Console.WriteLine(test(7, 87));    // Output: True
            Console.WriteLine(test(12, 45));   // Output: False

            Console.ReadLine();               // Keeping the console window open
        }

        // Method to check if the last digits of two integers are equal in absolute value
        public static bool test(int x, int y)
        {
            // Using Math.Abs to get the absolute value of the last digit of each integer and comparing them
            // Returns true if the absolute value of the last digits of x and y are equal, otherwise returns false
            return Math.Abs(x % 10) == Math.Abs(y % 10);
        }
    }
}

Sample Output:

False
True
True
False

Flowchart:

C# Sharp: Flowchart: Check if two given non-negative integers have the same last digit.

For more Practice: Solve these Related Problems:

  • Write a C# program to check whether the last digits of three integers are the same.
  • Write a C# program that returns true if two given integers have the same last digit and their sum ends with a zero.
  • Write a C# program to determine if two integers end with the same digit and their absolute difference is divisible by 10.
  • Write a C# program that checks if two numbers have the same last digit and are not divisible by 5.

Go to:


PREV : Contains 2-4 'z' Characters.
NEXT : Uppercase Last Three Characters.

C# Sharp Code Editor:



Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.