You are on page 1of 5

Soal2

Write a C program to find maximum number between three numbers using ladder if or nested
if. How to find maximum or minimum between three numbers using if else in C
programming. Finding maximum between three numbers using if statement in C program.
Logic to find maximum or minimum between three numbers in C program.

Example

Input

Input num1: 10
Input num2: 20
Input num3: 15

Output

Maximum is: 20

Required knowledge
Basic C programming, If else, Operators

Logic to find maximum between three numbers


In previous exercise we learnt to find maximum between two numbers.

Now, think logically if I gave a task to find maximum between three numbers a, b and c. How
will you find that? Let us get the step by step descriptive logic to find maximum between
three numbers.

1. Compare first two numbers a > b. If the statement is true. Then we only need to check
two more numbers for maximum i.e. a > c. If again it evaluates to true. Then a is
maximum otherwise c.

2. Suppose the statement a > b is false. Which means either b or c is maximum. Now
check one more condition b > c. It evaluates to true if b is maximum otherwise c.

There are various way to code this program. First let us observe this program using nested if

Program to find maximum between three number using


nested if
1 /**
2 * C program to find maximum between three numbers using nested if
*/
3
4 #include <stdio.h>
5
6
7
8
9
10 int
{
main()
11 int num1, num2, num3, maximum;
12
13 /* Input three numbers from user */
14 printf("Enter three numbers: ");
15 scanf("%d%d%d", &num1, &num2, &num3);
16
17
if(num1 > num2)
18 {
19 if(num1 > num3)
20 {
21 maximum = num1;
22 }
else
23 {
24 maximum = num3;
25 }
26 }
else
27
{
28 if(num2 > num3)
29 {
30 maximum = num2;
31 }
else
32 {
33 maximum = num3;
34 }
35 }
36
37 /* Prints the maximum value */
printf("Maximum among all three numbers = %d", maximum);
38
39 return 0;
40 }
41
42
43

The above method is lengthy and not recommended. Below is the simplest and recommended
method to use for these types of program. It uses the relational as well as logical operator to
find maximum.

Logic to find maximum using logical operator


We already know how to find maximum between two numbers using relational operator. Now
for three numbers observe the case. For given three numbers say a, b and c.
a is maximum only and only if a > b and a > c. If both statements are true then only a
is maximum.

Like above case b is maximum only and only if b > a and b > c.

Finally c is maximum only if both the conditions c > a and c > b are true.

Let us not implement this using logical operator and ladder if else.

Program to find maximum between three numbers using


logical operator
1
2
3 /**
4 * C program to find maximum between three numbers
5 */
6
7 #include <stdio.h>
8
9 int main()
{
10 int num1, num2, num3, maximum;
11
12 /*
13 * Input three numbers from user
14 */
printf("Enter three numbers: ");
15 scanf("%d%d%d", &num1, &num2, &num3);
16
17
18 /* If num1 is greater than both */
19 if((num1 > num2) && (num1 > num3))
20 {
21 maximum = num1;
}
22 else if((num2 > num1) && (num2 > num3))
23 {
24 maximum = num2;
25 }
else if((num3 > num1) && (num3 > num2))
26
{
27 maximum = num3;
28 }
29
30 /* Prints the maximum number */
31 printf("Maximum among all three numbers = %d\n", maximum);
32
return 0;
33 }
34
35
36
Another approach that will do the trick is mentioned below. I am leaving the logic of below to
you. Hope that it would be easy to get the concept of below program.

Program to find maximum using logical operator 2


1
2
3 /**
4 * C program to find maximum between three numbers
5 */
6
7 #include <stdio.h>
8
9 int
{
main()
10 int num1, num2, num3, maximum;
11
12 /*
13 * Input three numbers from user
14 */
printf("Enter three numbers: ");
15 scanf("%d%d%d", &num1, &num2, &num3);
16
17
18 if((num1 > num2) && (num1 > num3))
19 {
20 maximum = num1;
}
21
else if(num2 > num3)
22 {
23 maximum = num2;
24 }
25 else
{
26 maximum = num3;
27 }
28
29 /* Prints the maximum number */
30 printf("Maximum among all three numbers = %d\n", maximum);
31
32 return 0;
}
33
34
35

Advance your skills by learning this program using conditional operator.

You might also like