You are on page 1of 1

Check if a number is positive or negative Determine the sign of a number:

using the conditional operator: #include <stdio.h>


int main()
#include <stdio.h>
{
int main()
int num;
{
printf("Enter a number: ");
int num;
scanf("%d", &num);
printf("Enter a number: ");
(num >= 0) ? printf("%d is positive or zero.\n",
scanf("%d", &num);
(num >= 0) ? printf("%d is positive.\n", num) : num) : printf("%d is negative.\n", num);
printf("%d is negative.\n", num); return 0;
}
return 0;
}
SAMPLE PROGRAM:
Find the maximum of two numbers using #include <stdio.h>
void main()
the conditional operator:
{
#include <stdio.h>
int a=10, b=2
int main()
int s= (a>b) ? a:b;
{
int num1, num2, max; printf(“value is:%d”);
printf("Enter two numbers: "); }
scanf("%d %d", &num1, &num2);
max = (num1 > num2) ? num1 : num2; #include <stdio.h>
printf("Maximum number is: %d\n", max); int main()
return 0; {
} int num = 10;
Check if a number is even or odd using printf("%s\n", (num % 2 == 0) ? "Even" :
the conditional operator: "Odd");
#include <stdio.h> return 0;
int main() { }
int num;
printf("Enter a number: "); Check if a year is a leap year or not using
scanf("%d", &num); the conditional operator:
(num % 2 == 0) ? printf("%d is even.\n", num) #include <stdio.h>
: printf("%d is odd.\n", num); int main()
return 0; {
} int year;
Calculate the absolute value of a number printf("Enter a year: ");
using the conditional operator: scanf("%d", &year);
#include <stdio.h> ((year % 4 == 0 && year % 100 != 0) || (year
int main() % 400 == 0)) ? printf("%d is a leap year.\n",
{ year) : printf("%d is not a leap year.\n", year);
int num; return 0;
printf("Enter a number: "); }
scanf("%d", &num);
int abs_value = (num >= 0) ? num : -num;
printf("Absolute value of %d is: %d\n", num,
abs_value);
return 0;
}

You might also like