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

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_