Skip to content
Solved Examples · Example 24

Q.Write a C program to accept the three integers and print the largest amongst them. The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include<stdio.h>  
void main()  
{  
int x,y,z;  
printf("Enter the values of x,y,z\n");  
scanf("%d%d%d", &x, &y, &z);  
if (x>y);  
    if (x>z)  
        printf("x is largest\n");  
    else  
        printf("z is largest\n");  
else if (y>z)  
    printf("y is largest\n");  
else  
    printf("z is largest\n");  
}  
/*End of main()*/  
Sample output printed in the textbook:
Run 1                       Run 2  
Enter the values of x,y,z    Enter the values of x,y,z  
28                          -32  
55                          212  
58                          100  
Z is largest                y is largest  

Karnataka PUCTextbookLongImportance★★★★★est
73% · 112/154 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

[!TLDR]

Nested if-else compares three integers and prints the largest; 28,55,58 -> z and -32,212,100 -> y.

This is a nested-if (an if statement placed inside another) used to pick the maximum of three values. The outer test x > y narrows the contest to two candidates, and an inner test then compares the surviving candidate with z. Braces around the outer true branch are important so that the else clauses pair with the intended if. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.