# Conditional or Ternary Operator in C

Conditional or ternary operator in c; In this tutorial, i am going to show you conditional or ternary operators in c programming with examples.

This tutorial will take several examples as follows:

• C program to find maximum between two numbers using conditional operator
• To check whether the given number is positive or negative using conditional operator in c
• Find the given number is odd or even using the conditional operator in c
• Find the largest among three numbers using the conditional operator in C

## Conditional or Ternary Operator in C

C programming conditional operator is also known as a ternary operator. It takes three operands. Conditional operator is closely related with if..else statement.

Note that:- Conditional or ternary operators is used in the decision-making process.

### Syntax of Conditional Operator in C

Let’s see the syntax of conditional or ternary operator in c; is as follow:

`expression1 ? expression2 : expression3;`

or for simplicity, we write it as

`condition ? true-statement : false-statement;`

### Example 1 – C program to find maximum between two numbers using conditional operator

Using the conditional and ternary operator in c programming; you can find maximum between two numbers using conditional operator; as shown below:

```#include<stdio.h>
int main()
{
float num1, num2, max;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
max = (num1 > num2) ? num1 : num2;
printf("Maximum of %.2f and %.2f = %.2f",
num1, num2, max);
return 0;
}```

Output:-

```Enter two numbers: 12.5 10.5
Maximum of 12.50 and 10.50 = 12.50

```

Explanation of the above c program:

• First take input two number from user in program
• Then expression, (num1 > num2) is evaluated.
• num1=12.5 and num2=10.5; so expression (num1>num2) becomes true.
• Hence num1 is the maximum number, and it is assigned to the variable max.

### Example 2 – To check whether the given number is positive or negative using conditional operator in c

Using the conditional and ternary operator in c programming; you can check whether the given number is positive or negative using conditional operator in c ; as shown below:

```#include<stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num>=0)?printf("Positive."):printf("Negative");
return 0;
}```

Output:-

```Enter a number: 8
Positive.```

Explanation of the above c program:

• First take input number from user in program.
• Use ternary or conditional operator to find positive or negetive number.
• Print result.

### Example 3 – Find the given number is odd or even using the conditional operator in c

Using the conditional and ternary operator in c programming; You can find the given number is odd or even using the conditional operator ; as shown below:

```#include<stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num%2==0)? printf("Even"): printf("Odd");
return 0;
}```

Output:-

```Enter a number: 9
Odd```

Explanation of the above c program:

• First take input number from user in program.
• Use ternary or conditional operator to find even or odd number from user input number.
• Print result.

### Example 4 – Find the largest among three numbers using the conditional operator in C

Using the conditional and ternary operator in c programming; You can find the largest among three numbers using the conditional operator in c.; as shown below:

```#include<stdio.h>
int main()
{
float a, b, c, max;
printf("Enter three numbers: ");
scanf("%f %f %f",&a, &b, &c);
max = (a>b && a>b)? a: (b>a && b>c)? b:c;
printf("Maximum number = %.2f",max);
return 0;
}```

Output:-

```Enter three numbers: 10 30 12
Maximum number = 30.00```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7756366,"math_prob":0.9874033,"size":3603,"snap":"2022-27-2022-33","text_gpt3_token_len":876,"char_repetition_ratio":0.21116976,"word_repetition_ratio":0.39388794,"special_character_ratio":0.26533446,"punctuation_ratio":0.17475729,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9944959,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-07T16:20:29Z\",\"WARC-Record-ID\":\"<urn:uuid:5bbb547e-1366-4dca-828e-54bfc36b503d>\",\"Content-Length\":\"81008\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:db67a841-c665-4799-9ab4-348b7d0ad44c>\",\"WARC-Concurrent-To\":\"<urn:uuid:3eb55c18-c2bb-413f-b79d-bdbc68c7c81e>\",\"WARC-IP-Address\":\"104.21.83.247\",\"WARC-Target-URI\":\"https://laratutorials.com/conditional-operator-in-c-with-example/\",\"WARC-Payload-Digest\":\"sha1:XFUDVHPU3HT2OFF5COQOXUONCYOOJ3UM\",\"WARC-Block-Digest\":\"sha1:7YG2D2BSKVOC4CRZEO5RLTY2SXDG3BRG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104495692.77_warc_CC-MAIN-20220707154329-20220707184329-00129.warc.gz\"}"}