Electronics > Beginners

C Program to find maximum between three number using nested if

(1/4) > >>

khatus:
Step by step descriptive logic to find maximum between three numbers.

1 Input three numbers from user. Store it in some variable say num1, num2 and num3.

2. Compare first two numbers i.e. num1 > num2. If the statement is true then num2 is surely not max value. Perform one more comparison between num1 with num3 i.e. if(num1 > num3), then num1 is max otherwise num3.

3. If the statement num1>num2 is false. Which indicates that num1 is not max. Hence, this time compare num2 with num3. If the statement num2>num3 is true then num2 is max otherwise num3.


--- Code: ---/**
 * C program to find maximum between three numbers using nested if
 */

#include <stdio.h>

int main()
{
    int num1, num2, num3, max;

    /* Input three numbers from user */
    printf("Enter three numbers: ");
    scanf("%d%d%d", &num1, &num2, &num3);
   

    if(num1 > num2)
    {
        if(num1 > num3)
        {
            /* If num1 > num2 and num1 > num3 */
            max = num1;
        }
        else
        {
            /* If num1 > num2 but num1 > num3 is not true */
            max = num3;
        }
    }
    else
    {
        if(num2 > num3)
        {
            /* If num1 is not > num2 and num2 > num3 */
            max = num2;
        }
        else
        {
            /* If num1 is not > num2 and num2 > num3 */
            max = num3;
        }
    }
   
    /* Print maximum value */
    printf("Maximum among all three numbers = %d", max);

    return 0;
}
--- End code ---

Hello guys in this program if num1 = 3 num2 =2 and num3 = 1.Then the program first checks 3>2 if that satisfies then it checks 3>1 if this is not satisfies then max = num3;that means num3 =1 ids the greatest number.which is not possible.I am confused which line will be executed first


jesuscf:
max=num1;
if(num2>max) max=num2;
if(num3>max) max=num3;

oPossum:

--- Code: ---printf("Maximum among all three numbers = %d", (num1 < num2) ? ((num2 < num3) ? num3 : num2) : ((num1 < num3) ? num3 : num1));

--- End code ---

aix:
The mistake in your reasoning is here:


--- Quote ---then it checks 3>1 if this is not satisfies then max = num3

--- End quote ---

3 is greater than 1 and so the max is num1, not num3.

Personally, I'd write it the way jesuscf did.  Much easier to grasp at a glance and to reason about.

Lastly, your problem statement says "numbers".  Your code interprets that to mean "integers".  If one has to write similar code for floating-point numbers, there are additional subtleties to consider (such as NaNs and how they compare).

ve7xen:
Personally I would prefer to compose this from the standard two-input max function `int max(int a, int b)`.

So:


--- Code: ---int max(int a, int b) {
  return (a > b) ? a : b;
}

int main(void) {
  ...
  int max3 = max(num1, max(num2, num3));
  ...
}

--- End code ---

Or use C++ and the std::min and std::max library functions.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod