Jump to content

To find minimum of three numbers using ternary (conditional) operator

From IdeaWazaWiki
Revision as of 13:30, 31 January 2007 by wikademia>Mickraus (ANSI/ISO conformant; formatted)
/* To find the minimum of three numbers using the conditional operator */
#include <stdio.h>

int main(void)
{
    int a, b, c, temp, min;

    printf ("Enter three nos. separated by spaces: ");
    scanf ("%d%d%d", &a, &b, &c);

    temp = (a < b)    ? a : b;
    min =  (c < temp) ? c : temp;

    printf ("The Minimum of the three is: %d", min);

    /* indicate success */
    return 0;
}
/* Output:

 Enter three nos. separated by spaces: 2 5 9
 The Minimum of the three is: 2

*/