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

Popular posts from this blog

153: Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes. Note: The possible banknotes are 500, 100, 50, 20, 10, 5, 2, and 1

206: Write a program to create a class named "Circle" which has the property "radius". Define functions to calculate the area and circumference of the circle.

221: // In Task 2, we discussed multilevel inheritance with parameterized constructors for Student, UndergraduateStudent, and GraduateStudent classes in a university management system. Can you explain the advantages of using multilevel inheritance with specific details about the functions and data members in these classes? How were the parameterized constructors (e.g., setting student name, age, and ID) used to ensure that each class in the hierarchy correctly initializes its properties, such as creating an UndergraduateStudent named "John," aged 20, with a student ID of 12345