C#: Equality of three integers
C# Sharp Basic: Exercise-104 with Solution
Write a C# Sharp program to compare the equality of three integers and calculate how many integers have the same value.
Sample Data:
(1,2, 3) -> 0
(1,3,3) -> 2
(3,3,3) -> 3
Sample Solution:
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
// Initialize and assign values to variables x, y, and z
int x = 1, y = 2, z = 3;
// Display the original values of x, y, and z
Console.WriteLine("Original numbers: " + x + ',' + y + ',' + z);
// Output the number of equalities between the given numbers
Console.WriteLine("Number of equality of the said numbers: " + test(x, y, z));
// Assign new values to x, y, and z
x = 1; y = 3; z = 3;
// Display the new values of x, y, and z
Console.WriteLine("Original numbers: " + x + ',' + y + ',' + z);
// Output the number of equalities between the given numbers
Console.WriteLine("Number of equality of the said numbers: " + test(x, y, z));
// Assign new values to x, y, and z
x = 3; y = 3; z = 3;
// Display the new values of x, y, and z
Console.WriteLine("Original numbers: " + x + ',' + y + ',' + z);
// Output the number of equalities between the given numbers
Console.WriteLine("Number of equality of the said numbers: " + test(x, y, z));
}
// Method to determine the number of equalities among three integers
public static int test(int x, int y, int z)
{
// Return 3 if all three numbers are equal
// Return 0 if all three numbers are different
// Return 2 for any other case (two numbers are equal, but the third is different)
return (x == y && y == z) ? 3 : (x != y && y != z && x != z) ? 0 : 2;
}
}
}
Sample Output:
Original numbers: 1,2,3 Number of equality of the said numbers: 0 Original numbers: 1,3,3 Number of equality of the said numbers: 2 Original numbers: 3,3,3 Number of equality of the said numbers: 3
Flowchart:
C# Sharp Code Editor:
Previous C# Sharp Exercise: Sort characters in a string.
Next C# Sharp Exercise: C# Sharp Data Types Exercises.
What is the difficulty level of this exercise?
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://w3resource.com/csharp-exercises/basic/csharp-basic-exercise-104.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics