English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structures

C Language Files

C Others

C Language Reference Manual

C program to find the largest number among three numbers

Comprehensive Collection of C Programming Examples

In this example, you will learn how to find the largest number among the three numbers entered by the user.

To understand this example, you should know the followingC programmingTopic:

Example1Using if statement

#include <stdio.h>
int main() {
    double n1, n2, n3;
    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);
    //If n1is greater than n2and n3then n1is the largest
    if (n1 >= n2 && n1 >= n3{
        printf("%.2f is the largest number", n1);
    {}
    // If n2is also greater than n1and n3then n2is the largest
    if (n2 >= n1 && n2 >= n3{
        printf("%.2f is the largest number", n2);
    {}
    //If n3is greater than n1and n2, n3is the largest
    if (n3 >= n1 && n3 >= n2{
        printf("%.2f is the largest number", n3);
    {}        
    return 0;
{}

Example2Using if ... else ladder statements

#include <stdio.h>
int main() {
    double n1, n2, n3;
    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);
    //If n1is greater than n2and n3then n1is the largest
    if (n1 >= n2 && n1 >= n3) {
        printf("%.2f is the largest number", n1);
    // If n2is also greater than n1and n3then n2is the largest
    } else if (n2 >= n1 && n2 >= n3) {
        printf("%.2f is the largest number", n2);
        //If n3is greater than n1and n2, n3is the largest
    } else if (n3 >= n1 && n3 >= n2) {
        printf("%.2f is the largest number", n3);
    {}
    return 0;
{}

Example3Using nested if ... else

#include <stdio.h>
int main() {
    double n1, n2, n3;
    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);
    if (n1 >= n2) {
        if (n1 >= n3)
            printf("%.2lf is the largest number, n1);
        else
            printf("%.2lf is the largest number, n3);
    } else {
        if (n2 >= n3)
            printf("%.2lf is the largest number, n2);
        else
            printf("%.2lf is the largest number, n3);
    {}
    return 0;
{}

The output of all the above programs will be the same.

Enter three different numbers: 123.55
45.5
-454.6
123.55 Is the Largest Number

Comprehensive Collection of C Programming Examples