Flowchart diagram explaining if-else ladder control structure in C programming

The diagram shows the flowchart of the if-else ladder statement in C programming. It helps you visualize how the if-else-if ladder works by checking conditions one after another. When Condition1 is true, Statement1 runs, and the program moves on to the next part. But if Condition1 is false, it checks Condition2 next.

If Condition2 is true, Statement2 runs and then the program continues forward. This checking goes on down the ladder until it finds a condition that’s true. If none of the conditions are true, the program runs the default statement in the else block.


Example: Find the Biggest of Four Numbers

Let’s look at a simple example that finds the largest number among four inputs.

#include <stdio.h> int main() { int p, q, r, s; printf("\nEnter four numbers: "); scanf("%d %d %d %d", &p, &q, &r, &s); if (p > q && p > r && p > s) { printf("\n%d is the biggest number", p); } else if (q > p && q > r && q > s) { printf("\n%d is the biggest number", q); } else if (r > p && r > q && r > s) { printf("\n%d is the biggest number", r); } else { printf("\n%d is the biggest number", s); } return 0; }

Output:
Enter any four numbers
5
7
1
8
8 is Maximum number.

Program Explanation:
The above program illustrates how to use the if-else-if ladder statement to find the maximum number among four given numbers.
In this program, we declare four integer variables: int p, q, r, s; to store the input values.

Then, we use printf("\nEnter four numbers"); to show a message on the console, prompting the user to enter four numbers. The program waits for the input using scanf("%d%d%d%d", &p, &q, &r, &s);, which stores the entered numbers into variables p, q, r, and s respectively.

Next, we check if p is the largest number with the condition if(p > q && p > r && p > s). If this is true, the program prints that p is the biggest number.

If the above condition is false, the control moves to the next condition: else if(q > p && q > r && q > s). If this is true, it prints that q is the biggest number.
If that condition is also false, the program checks else if(r > p && r > q && r > s). If true, it prints that r is the biggest number.

Finally, if none of the above conditions are true, the else block executes and prints that s is the biggest number.
This step-by-step checking ensures the program correctly identifies the largest number among the four inputs.

Example Input:
5
7
1
8
Output:
8 is the biggest number.



Previous Topic:-->> if else statement in C || Next topic:-->>Nested if else in C.


Other Tutorials :
Topics wise Java Interview QuestionsTopics wise SQL QuestionsTopics Wise Python Interview Questions SQL Banking Case Study