Home > Back-end >  Write a program to find the greatest of four numbers
Write a program to find the greatest of four numbers

Time:01-06

I am new to Programming and currently learning C, so I don't know much about this concept but I was learning Conditional Instructions and at that time, my instructor [I am learning online from YouTube] explained me about logical operators.

He explained that logical operators are used with if...else to decrease the indentation and increase the readability.

After some time, I was solving a problem to find the greatest of four numbers and here, he contradicted the theory. He described nested if to solve this question instead of using logical operators.

Now; I am confused, what approach should I go with and why?

Also; when should I use arithmetic instructions and when should I use nested if...else?

Code written by me:

#include <stdio.h>

int main()
{
    int number1, number2, number3, number4;

    printf("\nEnter the vlaue of number1: ");
    scanf("%d", &number1);

    printf("\nEnter the value of number2: ");
    scanf("%d", &number2);

    printf("\nEnter the value of number3: ");
    scanf("%d", &number3);

    printf("\nEnter the value of number4: ");
    scanf("%d", &number4);

    if(number1 > number2 && number1 > number3 && number1 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number1);
    }

    else if(number2 > number3 && number2 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number2);
    }

    else if(number3 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number3);
    }

    else
    {
        printf("\n%d is the greatest of four numbers.\n", number4);
    }

    return 0;
}

Code written by my instructor:

#include <stdio.h>

int main()
{
    int number1, number2, number3, number4;

    printf("\nEnter the vlaue of number1: ");
    scanf("%d", &number1);

    printf("\nEnter the value of number2: ");
    scanf("%d", &number2);

    printf("\nEnter the value of number3: ");
    scanf("%d", &number3);

    printf("\nEnter the value of number4: ");
    scanf("%d", &number4);

    if(number1 > number2)
    {
        if(number1 >number3)
        {
            if(number1 > number4)
            {
                printf("\n%d is the greatest of four numbers.\n", number1);
            }
        }
    }

    else if(number2 > number3)
    {
        if(number2 > number4)
        {
            printf("\n%d is the greatest of four numbers.\n", number2);
        }
    }

    else if(number3 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number3);
    }

    else
    {
        printf("\n%d is the greatest of four numbers.\n", number4);
    }

    return 0;
}

CodePudding user response:

  1. The instructor’s code is wrong. If you enter 2, 1, 3, and 4 for the four numbers, it prints nothing, when it should print that 4 is the greatest.

  2. The logical operators, && and ||, are generally used to combine other conditions. Decreasing indentation and increasing readability is a separate goal. Do not fixate on either of these; simply learn to use the operators to perform desired computations and practice making your programs readable.

CodePudding user response:

Increasing indentation doesn't mean decrease readability. If you have a very long if-condition, it will decrease indentation, but also decrease readability. You should use the only way that allows you to keep your code clean and as simple as possible. Efficiency of code is also necessary since you are talking about C. Logical operators helps to combine two or more if-conditions in one and sometimes it really improve readability, sometimes - not. Sometimes, nested-ifs are better choice, you should think about your situation and choose the best way.

As stated by other commenters, your instructor is wrong and code has logical errors.

CodePudding user response:

If number1 = 5, number2 = 3, number3 = 6, number4 = 2

It would print 5 is the greatest of four numbers, whilst 6 is. So this is not correctly coded.

If conditional statements is the topic, your teacher has the more correct answer, however elaborate this may look.

Because on a practical note, you could code:

    /* be sure to #include <stdlib.h> */

    printf("\n%d is the greatest of four numbers.\n", 
          max(max(max(number1,number2),number3),number4);
  • Related