Lab Manual PPS

Find the largest of three numbers using nested if else. | PPS Program no.4 

Write a program to find the largest of three numbers using nested if else.

Code

#include <stdio.h>

int main()
{
    int a, b, c;
    printf("find the largest of three numbers\n");
    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;
}

Description

The program starts by including the standard input/output header file stdio.h.

In the main() function, three integer variables a, b, and c are declared to store the user input values. Two printf() statements are used to ask the user to enter three numbers.

The scanf() function is then used to read the user input values and store them in the corresponding variables a, b, and c. The & operator is used to get the memory address of the variables for input.

The program then uses nested if statements to compare the input values and determine the largest number. The outer if statement checks if a is greater than b. If it is, the inner if statement checks if a is also greater than c. If it is, a is printed as the largest number. Otherwise, c is printed as the largest number.

If a is not greater than b, then the else part of the outer if statement executes. This time, the inner if statement checks if b is greater than c. If it is, b is printed as the largest number. Otherwise, c is printed as the largest number.

Finally, the program returns 0 to indicate successful execution.

Output

find the largest of three numbers
enter three numbers:1 5 2
5 is the largest number.

In programming, a nested if-else statement is a construct used to create multiple levels of conditional branching within a program. It allows for a series of decisions to be made based on multiple conditions, with different outcomes for each possible combination of conditions.

A nested if-else statement consists of an outer if statement, which contains one or more inner if statements, each of which may contain additional inner if statements or else statements. The inner statements are executed only when their associated outer condition is true.

To summarize, nested if-else statements are a powerful tool for comparing multiple conditions in programming languages like C. By using these statements, we can easily find the largest of three numbers and output the result to the user.

Watch video for more detailed explanation.