Programa C para encontrar el número más grande entre tres números

Dados tres números A, B y C; La tarea es encontrar el número más grande entre los tres.

Ejemplos :

Input: A = 2, B = 8, C = 1
Output: Largest number = 8

Input: A = 231, B = 4751, C = 75821
Output: Largest number = 75821

En los siguientes programas, para encontrar el mayor de los tres números, sise utilizan si-más, if-else anidadoy operador ternario.

Algoritmo para encontrar el mayor de tres números:

1. Start
2. Read the three numbers to be compared, as A, B and C.
3. Check if A is greater than B.

  3.1 If true, then check if A is greater than C.
    3.1.1 If true, print 'A' as the greatest number.
    3.1.2 If false, print 'C' as the greatest number.

  3.2 If false, then check if B is greater than C.
    3.1.1 If true, print 'B' as the greatest number.
    3.1.2 If false, print 'C' as the greatest number.
4. End

Diagrama de flujo para encontrar el mayor de tres números:

A continuación se muestra el programa C para encontrar el mayor entre los tres números:

Ejemplo 1: Uso de sentencias if para encontrar el número más grande.

#include <stdio.h>
  
int main()
{
    int A, B, C;
  
    printf("Enter the numbers A, B and C: ");
    scanf("%d %d %d", &A, &B, &C);
  
    if (A >= B && A >= C)
        printf("%d is the largest number.", A);
  
    if (B >= A && B >= C)
        printf("%d is the largest number.", B);
  
    if (C >= A && C >= B)
        printf("%d is the largest number.", C);
  
    return 0;
}

Producción:

Enter the numbers A, B and C: 2 8 1 
8 is the largest number.

Ejemplo 2: Usar la declaración if-else para encontrar el número más grande.

#include <stdio.h>
int main()
{
    int A, B, C;
  
    printf("Enter three numbers: ");
    scanf("%d %d %d", &A, &B, &C);
  
    if (A >= B) {
        if (A >= C)
            printf("%d is the largest number.", A);
        else
            printf("%d is the largest number.", C);
    }
    else {
        if (B >= C)
            printf("%d is the largest number.", B);
        else
            printf("%d is the largest number.", C);
    }
  
    return 0;
}

Producción:

Enter the numbers A, B and C: 2 8 1 
8 is the largest number.

Ejemplo 3: uso de sentencias if-else anidadas para encontrar el número más grande.

#include <stdio.h>
int main()
{
    int A, B, C;
  
    printf("Enter three numbers: ");
    scanf("%d %d %d", &A, &B, &C);
  
    if (A >= B && A >= C)
        printf("%d is the largest number.", A);
  
    else if (B >= A && B >= C)
        printf("%d is the largest number.", B);
  
    else
        printf("%d is the largest number.", C);
  
    return 0;
}

Producción:

Enter the numbers A, B and C: 2 8 1 
8 is the largest number.

Ejemplo 4: uso del operador ternario para encontrar el número más grande.

#include <stdio.h>
int main()
{
    int A, B, C, largest;
  
    printf("Enter three numbers: ");
    scanf("%d %d %d", &A, &B, &C);
  
    largest = A > B ? (A > C ? A : C) : (B > C ? B : C);
  
    printf("%d is the largest number.", largest);
  
    return 0;
}

Producción:

Enter the numbers A, B and C: 2 8 1 
8 is the largest number.

Publicación traducida automáticamente

Artículo escrito por RishabhPrabhu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *