Question:

C program. i need <span title="help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1?">help!!!!!!!!!!!!!!!!!!!!!...</span>

by  |  earlier

0 LIKES UnLike

pls can any1 tell me how to do this program.

find the maximum and minimum of three numbers using

1)ternary operator

2) if...else if

 Tags:

   Report

3 ANSWERS


  1. int main(void)

    { int a,b,c, max, min;

    max = a&gt;b?a:b;

    max = max&gt;c?max:c;

    printf(&quot;max : %d\n&quot;, max);

    min = a&lt;b?a:b;

    min = min&lt;c?min:c;

    printf(&quot;min : %d\n&quot;, min);

    return 0;

    }

    this is using ternary operators, now usin if else is as below :

    int main(void)

    { int a,b,c,max,min;

    if(a&gt;b)

      if(a&gt;c)

       max = a;

      else

       max = c;

    else if(b&gt;c)

      max = b;

    else

      max = c;

    if(a&lt;b)

      if(a&lt;c)

       min = a;

      else

       min = c;

    else if(b&lt;c)

      min = b;

    else

      min = c;

    printf(&quot;max : %d \t min : %d\n&quot;), max, min);

    return 0;

    }


  2. This looks like a homework program so I won&#039;t do the work for you, but here is what you are looking at.  First you need to get three numbers (presumably from a user), you should know how to do this.

    Second, you need to compare the three.  You do this by creating a integer to hold your maximum value (say, call it max), and set it equal to your first number (int max = num1).  Then you need to find your max using both approaches listed.

    You will just need to compare the second number to &quot;max&quot; and whichever is greatest is the new max, then do the same with the third number.

    In the first case it will look something like:

    max = (num2 &gt; max) ? num2 : max;

    The second case will look like:

    if(num2 &gt; max)

    max = num2;

    Good luck.


  3. USING IF....ELSE IF

    main()

    {

    int a,b,c;

    if((a&gt;b)&amp;&amp;(a&gt;c))

    {

    printf(&quot;a is largest&quot;);

    }

    else if((b&gt;c)&amp;&amp;(b&gt;a))

    {

    printf(&quot;b is largest&quot;);

    }

    else

    printf(&quot;c is largest&quot;);

    return 0;

    }

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions