C#: Check whether two or more non-negative given integers have the same rightmost digit
C# Sharp Basic Algorithm: Exercise-50 with Solution
Same Rightmost Digit in Two or More Integers
Write a C# Sharp program to check if two or more integers that are not negative have the same rightmost digit.
Visual Presentation:
Sample Solution:-
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
// Class declaration
class Program
{
// Main method - entry point of the program
static void Main(string[] args)
{
// Printing the results of the 'test' method with different integer values
Console.WriteLine(test(11, 21, 31)); // Output: True (since at least two numbers have the same last digit)
Console.WriteLine(test(11, 22, 31)); // Output: True (since at least two numbers have the same last digit)
Console.WriteLine(test(11, 22, 33)); // Output: False (since all numbers have different last digits)
Console.ReadLine(); // Keeping the console window open
}
// Method to check if at least two integers have the same last digit
public static bool test(int x, int y, int z)
{
// Returns true if x's last digit equals y's last digit or x's last digit equals z's last digit or y's last digit equals z's last digit
return x % 10 == y % 10 || x % 10 == z % 10 || y % 10 == z % 10;
}
}
}
Sample Output:
True True False
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to check whether three given numbers are in strict increasing order, such as 4 7 15, or 45, 56, 67, but not 4 ,5, 8 or 6, 6, 8.However,if a fourth parameter is true, equality is allowed, such as 6, 6, 8 or 7, 7, 7.
Next: Write a C# Sharp program to check three given integers and return true if one of them is 20 or more less than one of the others.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/basic-algo/csharp-basic-algorithm-exercises-50.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics