Thursday, December 22, 2011

Using Ternary Operator


/* A program to read a number and print -1 if it is negative, 0 if it is zero and 1 if it is positive */

#include<stdio.h>
int main()

{
int a,b;

printf("\n The Program will print :\n 1 if input number is +ve\n 0 if input number is 0\n -1 if input number is -ve\n");
printf("Enter a number:\n");
scanf("%d",&a);

b=a>0?1:(a<0?-1:0);
printf("\nResult = %d",b);

return 0;

}

No comments:

Post a Comment