Pages

Find Greatest and Lowest Number using Ternary Operator in C

This program give you some idea to use ternary operator in C. We can use this operator in place of IF....ELSE. In this example we use this to find greatest and lowest number among 3 number.

Source Code:
#include <stdio.h>
#include <conio.h>

void main()
{
    int a, b, c, sml, grt, sum;
    float avg;

    printf("Greatest and Lowest Number using Tarnary Opreateor\n");
    printf("\n-----------------------------------------------------\n");
    printf("Enter 3 no: ");
    scanf("%d %d %d",&a, &b, &c);

     sum = a + b + c;
     avg = (float)sum/3;
     grt = (a > b && b > c )? a : ( b > c && b > a)? b : c;
     sml = (a < b && b < c)? a : (b < c && b < a)? b : c;

     printf("\nSum = %d \nAverage = %.3f", sum, avg);
     printf("\nGreater = %d", grt);
     printf("\nSmallest = %d", sml);
     getch();
     return 0;
}


Output:

@msucil

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment