Table of Contents
Introduction
This program will find the largest number of the three numbers entered by the user.
Here we will use an if-else statement.
To understand this program, you should know about
1. C programming operators
2. C nested if…else statement
Program
#include<stdio.h> int main() { int x,y,z; printf("enter three numbers:\n"); scanf("%d %d %d",&x,&y,&z); if(x>y && x>z) // && operator will give true only if both the conditions are true { printf("%d is the greatest number",x); } else if(y>x && y>z) { printf("%d is the greatest number",y); } else // no need to write any condition after ‘else’ { printf("%d is the greatest number",z); } return 0; }
Output
Explanation
In the above code, we take three values from the user and store them in x, y, and z respectively.
if(x>y && x>z) in this statement ‘x’ is compared with ‘y’ and ‘z’. Since we use && operator here thus, if both conditions are true, then the block of this statement will be executed.
The values entered by the user don’t satisfy the first if statement.
Then it will move to the next statement, which is,
else if(y>x && y>z)
98>65 && 98>65, thus both conditions are true hence its block of code will be executed, and y(=98) is greatest among all three and gets printed.
0 Comments