0% found this document useful (0 votes)
12 views1 page

Operator in C5

This document contains examples of using the conditional operator in C to check if a number is positive or negative, find the maximum of two numbers, check if a number is even or odd, calculate the absolute value of a number, and check if a year is a leap year or not.

Uploaded by

Hema Malini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Operator in C5

This document contains examples of using the conditional operator in C to check if a number is positive or negative, find the maximum of two numbers, check if a number is even or odd, calculate the absolute value of a number, and check if a year is a leap year or not.

Uploaded by

Hema Malini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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