Positive or Negative or Zero Using Ternary Operator: C Program


C Program to check whether the user entered integer number is positive, negative or zero using ternary operator or Conditional operator.

Related Read:
Number is Positive or Negative or Zero: C Program

Check Whether Number Is Positive or Negative or Zero Using Ternary Operator: C Program



YouTube Link: https://www.youtube.com/watch?v=gYenZ8rYK8g [Watch the Video In Full Screen.]


Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

Check Whether Number is Positive or Negative or Zero: C Program

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int a;  
  6.   
  7.     printf("Enter an integer number\n");  
  8.     scanf("%d", &a);  
  9.   
  10.     (a > 0) ?  
  11.     (printf("%d is positive\n", a)) :  
  12.     ( (a < 0) ?  
  13.       (printf("%d is Negative\n", a)) :  
  14.       (printf("%d is Zero\n", a))    
  15.     );  
  16.   
  17.     return 0;  
  18. }  

Output 1:
Enter an integer number
15
15 is positive

Output 2:
Enter an integer number
-2
-2 is negative

Output 3:
Enter an integer number
0
0 is zero

In above C source code, we are using nested ternary / conditional operator. First we check if a is greater than 0, if its true then the user entered number is positive. If its false, then we check if a is less than 0 using nested ternary / conditional operator. If that is true, then a is negative, else the user entered number is 0.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *