# Write a C Program to find maximum between 3 numbers.

Write a C Program to find maximum between 3 numbers.

We will learn how we can find the maximum of 3 numbers in c/c++. The code will work in all languages with few modifications.

###### Prerequisite Knowledge
1. Relational Operator
2. Ternary/Conditional Operator
3. if..else Statement
4. Basic C/C++ Programming knowledge

The problem can be solved in various way. We will code 4 of them.

The basic idea is first we compare the first 2 numbers using the if statement, then we compare the greater of the first 2 numbers with the third number.

### Program to find maximum between 3 numbers Using if Statement

Let x, y, z be the three numbers

#### Steps

1. If x is greater than both y and z, print x and exit the program.
2. If y is greater than both x and z, print y and exit the program.
3. If z is greater than both x and y, print z, and exit the program.
```#include<stdio.h>

int main(){
int x,y,z;
printf(" Enter First Number: ");
scanf("%d",&x);
printf(" Enter Second Number: ");
scanf("%d",&y);
printf(" Enter Third Number: ");
scanf("%d",&z);

if(x>=y&&x>=z){
printf(" Largest Number: %d", x);
return 0;
}

if(y>=x&&y>=z){
printf(" Largest Number: %d", y);
return 0;
}

if(z>=y&&z>=x){
printf(" Largest Number: %d", z);
return 0;
}

}```

#### Output

```Enter First Number: 1
Enter Second Number: 5
Enter Third Number: 3
Largest Number: 5```

### Program to find maximum between 3 numbers Using if…else Statement

This method is very similar to previous method. The only difference is, here we do not need to use return statement three times since we are using if..else.

```#include<stdio.h>

int main(){
int x,y,z;
printf(" Enter First Number: ");
scanf("%d",&x);
printf(" Enter Second Number: ");
scanf("%d",&y);
printf(" Enter Third Number: ");
scanf("%d",&z);

if(x>=y&&x>=z){
printf(" Largest Number: %d", x);
}
else if(y>=x&&y>=z){
printf(" Largest Number: %d", y);
}
else{
printf(" Largest Number: %d", z);
}
return 0;
}```

#### Output

```Enter First Number: 8
Enter Second Number: 10
Enter Third Number: 90
Largest Number: 90```

### Program to find maximum between 3 numbers Using nested if..else Statement

#### Steps

1. If x is greater than y
1. If x is greater than z
1. Print x
2. if x is less than equal to z
1. Print z
2. if x is less than equal to y
1. If y is greater than z
1. Print y
2. If y is less than equal to z
1. Print z
3. Return
```#include<stdio.h>

int main(){
int x,y,z;
printf(" Enter First Number: ");
scanf("%d",&x);
printf(" Enter Second Number: ");
scanf("%d",&y);
printf(" Enter Third Number: ");
scanf("%d",&z);

if(x>y){
if(x>z){
printf(" Largest Number: %d", x);
}
else{
printf(" Largest Number: %d", z);
}
}
else{
if(y>z){
printf(" Largest Number: %d", y);
}
else{
printf(" Largest Number: %d", z);
}
}

return 0;
}```

#### Output

```Enter First Number: 50
Enter Second Number: 90
Enter Third Number: 500
Largest Number: 500```

### Program to find maximum between 3 numbers Using Conditional/Ternary Operator

```#include<stdio.h>

int main(){
int x,y,z;
int result;

printf(" Enter First Number: ");
scanf("%d",&x);
printf(" Enter Second Number: ");
scanf("%d",&y);
printf(" Enter Third Number: ");
scanf("%d",&z);

result = (x>y)?x:y;
result = (z>result)?z:result;

// You can also directly use
// result = (x>y)?(x>z)?x:z:(y>z)?y:z;

printf(" Largest Number: %d", result);

return 0;
}```

#### Output

```Enter First Number: 1000
Enter Second Number: 500
Enter Third Number: 700
Largest Number: 1000```