116: Write a program that takes three numbers as input and determines the largest number among them. If two or more numbers are equal, display anappropriate message
// Write a program that takes three numbers as input and determines the largest number among them.
// If two or more numbers are equal, display anappropriate message
#include <iostream>
using namespace std;
int inum(string x);
bool unequal(int x, int y, int z);
bool anyequal(int x, int y, int z);
int larger(int x, int y, int z);
void equal(int x, int y, int z);
int main()
{
int num1 = inum("Enter first number: "), num2 = inum("Enter second number: "),
num3 = inum("Enter third number: ");
if (unequal(num1, num2, num3))
{
cout << "The greatest number among these is: " << larger(num1, num2, num3) << endl;
if (anyequal(num1, num2, num3))
{
equal(num1, num2, num3);
}
}
else
{
cout << "All three numbers ie first number(" << num1 << "), second number(" << num2
<< ") and third number(" << num3 << ") are equal\n";
}
return 0;
}
int inum(string x)
{
int y;
cout << x;
cin >> y;
return y;
}
bool unequal(int x, int y, int z)
{
return !(x == y && x == z); // function will not run only in this condition.
}
bool anyequal(int x, int y, int z)
{
return ((x == y) || (x == z) || (y == z));
}
int larger(int x, int y, int z)
{
if (x > y)
{
if (x > z)
{
return x;
}
else
{
return z;
}
}
else if (x < y)
{
if (y > z)
{
return y;
}
else
{
return z;
}
}
else if (x == y)
{
if (x > z)
{
return x;
}
else
{
return z;
}
}
else if (x == z)
{
if (x > y)
{
return x;
}
else
{
return y;
}
}
else if (y == z)
{
if (y > x)
{
return y;
}
else
{
return x;
}
}
else
{
return 0;
}
}
void equal(int x, int y, int z)
{
if (x == y)
{
cout << "First number(" << x << ") is equal to second number(" << y << ")\n";
}
else if (x == z)
{
cout << "First number(" << x << ") is equal to third number(" << z << ")\n";
}
else if (y == z)
{
cout << "Second number(" << y << ") is equal to third number(" << z << ")\n";
}
}
Comments
Post a Comment