Wap to find largest of three number in C


Here are the two best methods to find the largest of three numbers in C.

The code are here as follow:-

Method 1: Using if–else

#include <stdio.h>

int main() 

{

    int a, b, c;

    printf("Enter three numbers: \n");

    scanf("%d %d %d", &a, &b, &c);

    if (a >= b && a >= c) {

        printf("%d is the largest.", a);

    }

    else if (b >= a && b >= c) {

        printf("%d is the largest.", b);

    }

    else 

{

        printf("%d is the largest.", c);

    }


    return 0;

}


Method 2: Using Logical Operators

#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.", a); else printf("%d is the largest.", c); } else { if (b > c) printf("%d is the largest.", b); else printf("%d is the largest.", c); } return 0; }


Previous Post Next Post

Contact Form