Ticker

6/recent/ticker-posts

Program To Find Maximum Among 3 Numbers in C/C++

Program To Find Maximum Among 3 Numbers in C++:

There are various ways to find the maximum among 3 numbers. Some of the Algorithms are given below:

Algorithm 1:

Step 1: Accept three numbers say A, B&C.
Step 2: 
If A>B & A>C then Display A is greatest
else if B>C then Display B is greatest
else Display C is greatest
Step 3: Stop

Algorithm 2:

Step 1: Accept three numbers say A, B&C.
Step 2: 
If A>B & A>C then Display A is greatest
else if B>A&B>C then Display B is greatest
else Display C is greatest
Step 3: Stop

Algorithm 3:

Step 1: Accept three numbers say A, B&C.
Step 2: 
If A>B 
then check If A>C then Display A is greatest else Display C is greatest
else 
check If B>C then Display B is greatest else Display C is greatest
Step 3: Stop

The above Algorithms can be implemented in C++ as follows:

Code 1:


#include <iostream>
using namespace std;
int main()
{
    int abc;
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;
    cout << "Enter third number: ";
    cin >> c;
    if (a > b && a > c)
        cout << "Max is A";
    else if (b > a && b > c)
        cout << "Max is B";
    else
        cout << "Max is C";
    return 0;
}

Code 2:


#include <iostream>
using namespace std;
int main()
{
    int abc;
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;
    cout << "Enter third number: ";
    cin >> c;
    if (a == b && a == c)
        cout << "All the numbers are equal";
    else if (a == b || b == c || c == a)
        cout << "Two numbers are equal";
    else
    {
        if (a > b && a > c)
            cout << "Max is A";
        else if (b > c)
            cout << "Max is B";
        else
            cout << "Max is C";
    }
    return 0;
}


Code 3:


#include <iostream>
using namespace std;
int main()
{
    int abc;
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;
    cout << "Enter third number: ";
    cin >> c;
    if (a > b)
    {
        if (a > c)
            cout << "Max is A";
        else
            cout << "Max is C";
    }
    else
    {
        if (b > c)
            cout << "Max is B";
        else
            cout << "Max is C";
    }
    return 0;
}

There is an alternate for using if-else statements in C++ which is a ternary operator. So we can make a program by using the ternary operator also. Following is the example of a ternary operator program in C++ to find maximum between three numbers.

Using Ternary operator

Code:


#include <iostream>
using namespace std;
int main()
{
    int abcmax;
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;
    cout << "Enter third number: ";
    cin >> c;
    max = (a > b && a > c) ? a : (b > c) ? b : c;
    cout << "Maximum value among three numbers is= " << max;
    return 0;
}

Program in C to Find Maximum Among 3 Numbers:

The Algorithms can be implemented in C as follows:

Code 1:


#include <stdio.h>
int main()
{
    int abc;
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);
    printf("Enter third number: ");
    scanf("%d", &c);
    if (a == b && a == c)
        printf("All the numbers are equal");
    else if (a == b || b == c || c == a)
        printf("Two numbers are equal");
    else
    {
        if (a > b && a > c)
            printf("Max is A");
        else if (b > c)
            printf("Max is B");
        else
            printf("Max is C");
    }
    return 0;
}

Code 2:


#include <stdio.h>
int main()
{
    int a, b, c;
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);
    printf("Enter third number: ");
    scanf("%d", &c);
    if (a > b)
    {
        if (a > c)
            printf("Max is A");
        else
            printf("Maxi is C");
    }
    else
    {
        if (b > c)
            printf("Max is B");
        else
            printf("Max is C");
    }
    return 0;
}


Using Ternary operator

Code:


#include <stdio.h>
int main()
{
    int abcmax;
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);
    printf("Enter third number: ");
    scanf("%d", &c);
    max = (a > b && a > c) ? a : (b > c) ? b : c;
    printf("Maximum value among three numbers is= %d"max);
    return 0;
}


If you have any doubts/questions related to Program, feel free to comment below. Don't forget to subscribe and share it with friends.

Post a Comment

0 Comments