C program to find the largest number among three numbers (Using, if-else statement)

by | Jan 11, 2023 | C, C Programs

Home » C » C Programs » C program to find the largest number among three numbers (Using, if-else statement)

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.

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author