Largest numbers in Three numbers C Programming Example

Algorithm


  • Take three number, n1, n2 and n3
  • n1 is largest, if n1 >= n2  && n1 >= n3
  • n2 is largest, if n2 >= n1  && n2 >= n3
  • n3 is largest, if n3 >= n1  && n3 >= n2
  • Print the largest number

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <stdio.h>

int main() {
    float n1, n2, n3;
    
    printf("Enter three different numbers: ");
    scanf("%f %f %f\n", &n1, &n2, &n3);

   
    if (n1 >= n2 && n1 >= n3) {  // if n1 is greater than both n2 and n3, n1 is the largest
        printf("%.2f is the largest number.", n1);
    } else if (n2 >= n1 && n2 >= n3) { // if n2 is greater than both n1 and n3, n2 is the largest
        printf("%.2f is the largest number.", n2);
    } else if (n3 >= n1 && n3 >= n2) {  // if n3 is greater than both n1 and n2, n3 is the largest
        printf("%.2f is the largest number.", n3);
    }

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Enter three different numbers: 10 40 5

Output

x
+
cmd
10.00 is the largest number.

Demonstration


We've used a very simple if-else to find the largest number in three numbers.