0% found this document useful (0 votes)
41 views

Operator in c5

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Operator in c5

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 DOCX, 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) : 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy