C Programming: Ternary Operator


SUBMITTED BY: Guest

DATE: Nov. 13, 2013, 4:43 p.m.

FORMAT: C++

SIZE: 1.1 kB

HITS: 841

  1. // PURPOSE : Print out the biggest and lowest DISTINCT values using the TERNARY operator
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int a, b, c;
  6. int max;
  7. int min;
  8. int salary;
  9. float bonus;
  10. printf("\n Please enter DISTINCT values for a, b, and c : ");
  11. scanf_s("%d %d %d", &a, &b, &c);
  12. // PRINTING the bigger of the two numbers using if-else statement
  13. // i.e., without the use of the TERNARY operator
  14. if (a > b)
  15. {
  16. printf("\n\n %d is bigger than %d", a, b);
  17. }
  18. else
  19. {
  20. printf("\n\n %d is bigger than %d", b, a);
  21. }
  22. // PRINTING the bigger number using the TERNARY operator
  23. max= (a > b) ? a : b;
  24. printf("\n The biggest of %d and %d is %d", a, b, max);
  25. max = (a > b) ? ( (a > c) ? a : c) : ( ( b > c) ? b : c);
  26. printf("\n The biggest of %d, %d, and %d is %d", a, b, c, max);
  27. min = (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c);
  28. printf("\n The smallest of %d, %d, and %d is %d", a, b, c, min);
  29. getchar();
  30. getchar();
  31. return 0;
  32. }

comments powered by Disqus