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