C Program to Find Greatest Number Among Three.

Given three numbers a, b, and c. Your task is to write a C program to find the largest number among three given numbers. The program should take three integer inputs from the user and determine which number is the largest among them.

Example:

Input: a = 10, b = 5, c = 9
Output: Largest Number = 10

Input: a = -5, b = 2, c = 1
Output: Largest Number = 2

Steps to Find the Greatest Number Among Three:

Step 1: Input three numbers (num1, num2, num3) from the user.
Step 2: Compare the three numbers to find the greatest among them.
Step 3: Display the greatest number as the result.

C code to find the Greatest number using if-else.
//C program to find the greatest number among three
#include <stdio.h>

int main() {
    int a, b, c, greatest;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    if (a > b && a > c) {
        greatest = a;
    } else if (b > a && b > c) {
        greatest = b;
    } else {
        greatest = c;
    }

    printf("The greatest number among three is: %d\n", greatest);

    return 0;
}
Output:
Enter three numbers: 3 6 1
The greatest number among three is: 6


C Code to find the greatest number among three using Ternary Operator.
//C program to find the greatest number using ternary operator
#include <stdio.h>

int main() {
    int a, b, c, greatest;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    // ternary operator to find greatest
    greatest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

    printf("The greatest number is: %d\n", greatest);

    return 0;
}
Output:
Enter three numbers: 10 20 40
The greatest number is: 40

Explanation:

To find the greatest number among three given numbers, we compare the numbers in pairs. We first compare num1 and num2, then num1 and num3, and finally num2 and num3. The number that is greater than the other two in each comparison is the greatest number among the three.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS