You are here:   HomeC Programming (Theory)Control Structures in CWrite a C program to calculate greatest number using nested if, out of three.
by Dinesh Thakur Category: Control Structures

A program to determine the maximum of three numbers, a, band c, is given in Program in which we first determined the larger of two numbers (a and b) which was then compared with the third number, c. Here, another technique is presented, the algorithm for which is given below.

if(a>b)

max = larger of a and c

else max = larger of b and c

Thus, if a is greater than b, the larger of a and c is the maximum number; otherwise, the maximum number is the larger of b and c. If we use an if-else statement to determine the larger of two numbers in the above algorithm, we get a two-level nested if statement to determine maximum of three numbers as shown below.

/* determine maximum of 3 numbers */

if (a > b} {

    if (a > c)

        max = a;

   else max = c;

}

else {

if (b > c)

    max = b;

    else max = c;

 

Example of calculate greatest number using nested if

 

#include<stdio.h>
#include<conio.h>
void main()
{
     int x,y,z;
     clrscr();
     printf("\n Enter the value of x,y,z:");
     scanf("%d%d%d",&x,&y,&z);
         if(x>y)
         if(x>z)
           printf("\n X = %d is Greatest",x);
         else
          printf("\n Z = %d is Greatest",z);
         else
         if(y>z)
             printf("\n Y = %d is Greatest",y);
         else
           printf("\n Z = %d is Greatest",z);
         getch();
}
Enter the value of x,y,z: 23 43 45
Z = 45 is Greatest


Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article