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

215: Create a C++ program that uses single inheritance to model a simple shape hierarchy. Implement a base class "Shape" with a virtual function "printArea." Derive classes "Rectangle" and "Triangle" from "Shape" and override the "printArea" function to calculate and display their respective areas.

42: ***(Syntax)*** Calculating number of characters and spaces in a string 1- Make a program to calculate the number of characters in a string 2- Modify this and also tell how many spaces are there in that sentence

212: Build a class representing a university student with private data members for name, ID, and a static data member for the total number of students. Implement getter and setter functions for name and ID, and a static getter function to retrieve the total number of students. Use default constructors