expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.
Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.
Expected Output for the Input
User Input: Enter the salary 40000
Output: Manager
Logic To Convert from if else To Conditional Operator
As you can see we’ve nested if else statement inside first else statement. So we’ll have nested conditional operator inside expression_3 of outer conditional operator.
Video Tutorial: C Program To Determine Designation of Employee Based On Salary
#include<stdio.h>
int main()
{
int sal;
printf("Enter the salary\n");
scanf("%d", &sal);
( sal >= 25000 && sal <= 40000 ) ?
printf("Manager\n") :
( sal >= 15000 && sal < 25000)?
printf("Accountant\n") :
printf("Clerk\n");
return 0;
}
Output 1: Enter the salary 35000 Manager
Output 2: Enter the salary 20000 Accountant
Output 3: Enter the salary 10000 Clerk
Note: As you can see if you enter the salary as $40001 and above, it shows the designation as Clerk. That is because we’re not checking for salary above $40000. With the problem statement given above, we assume that this C program is for employees with salary less than $40000 and for designation Manager, Accountant and Clerk only.
expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.
Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.
Expected Output for the Input
User Input: Enter a Character $
Output: Character Entered Is a Special Symbol
Logic To Find Special Symbol or Not using Conditional Operator
Using Conditional Operator we write the condition, if user entered character is in between or equal to ASCII values 0 – 47 or 58 to 64 or 91 to 96 or greater than or equal to 123. We use Relational Operator and Logical Operators to accomplish the task.
If the condition in expression_1 is true, then whatever code is present in expression_2 gets executed. If condition is expression_1 is false then the code present in expression_3 gets executed.
Video Tutorial: C Program To Find Character is Special Symbol or Not using Conditional Operator
Output: Character entered is a lowercase English alphabet
Logic To Find Lowercase Alphabet or Not using Conditional Operator
Using Conditional Operator we write the condition, if user entered character is greater than or equal to ASCII Value 97(which corresponds to lowercase character a) and less than or equal to ASCII Value 122(which corresponds to lowercase character z).
If the condition is true, then user entered character is lower case English alphabet, if not, then its not a lowercase English alphabet.
Video Tutorial: C Program To Find Lowercase Alphabet or Not using Conditional Operator
printf("Character entered is a lowercase English alphabet\n") :
printf("Character entered is not a lowercase English alphabet\n");
return 0;
}
#include < stdio.h >
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
(ch >= 97 && ch <= 122) ?
printf("Character entered is a lowercase English alphabet\n") :
printf("Character entered is not a lowercase English alphabet\n");
return 0;
}
Output 1: Enter a character $ Character entered is not a lowercase English alphabet
Output 2: Enter a character A Character entered is not a lowercase English alphabet
Output 3: Enter a character 5 Character entered is not a lowercase English alphabet
Output 4: Enter a character a Character entered is a lowercase English alphabet
Output 5: Enter a character z Character entered is a lowercase English alphabet
Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, using Ternary Operator or Conditional Operator and without using arrays.
According to the Gregorian Calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.
Note: From 01/01 we understand that it’s 1st of January. We shall take the year as 1900. We also know that 01 January 1900 was Monday.
Logic To Find The Day on 01 January using Gregorian Calendar
We take reference year as 1900. We ask the user to enter a year between 1900 and 2099 and store it inside the address of variable year. Now we find the difference (1900 – year) and store it inside variable diff.
Using while loop, we loop through until reference year(1900) is less than the user entered year. Inside the while loop we check for all the leap years present from 1900 to user entered year.
Next we calculate exact number of days between 1900 and user entered year using formula:
diff = ref_year – year; Total no of leap years is calculated and stored in variable leap.
Total_days = (diff – leap) x 365 + leap x 366;
Now every year has 12 months, but each month has different number of days. But every week has exactly 7 days. So we divide Total_days by 7 and store the reminder inside variable day.
Note: Since our reference year 1900 has Monday on 1st of January. So we take Monday as first day:
int ref_year = 1900, year, leap = 0, diff, total_days = 0, day = 0;
printf("Enter a year between 1900 and 2099\n");
scanf("%d", &year);
diff = year - ref_year;
while(ref_year < year)
{
if(ref_year % 100 == 0)
{
if(ref_year % 400 == 0)
{
leap++;
}
}
else
{
if(ref_year % 4 == 0)
{
leap++;
}
}
ref_year++;
}
total_days = (diff - leap) * 365 + leap * 366;
day = total_days % 7;
printf("\nThe day on 01 January %d was ", year);
switch(day)
{
case 0: printf("Monday.\n");
break;
case 1: printf("Tuesday.\n");
break;
case 2: printf("Wednesday.\n");
break;
case 3: printf("Thursday.\n");
break;
case 4: printf("Friday.\n");
break;
case 5: printf("Saturday.\n");
break;
case 6: printf("Sunday.\n");
break;
}
return 0;
}
#include < stdio.h >
int main()
{
int ref_year = 1900, year, leap = 0, diff, total_days = 0, day = 0;
printf("Enter a year between 1900 and 2099\n");
scanf("%d", &year);
diff = year - ref_year;
while(ref_year < year)
{
if(ref_year % 100 == 0)
{
if(ref_year % 400 == 0)
{
leap++;
}
}
else
{
if(ref_year % 4 == 0)
{
leap++;
}
}
ref_year++;
}
total_days = (diff - leap) * 365 + leap * 366;
day = total_days % 7;
printf("\nThe day on 01 January %d was ", year);
switch(day)
{
case 0: printf("Monday.\n");
break;
case 1: printf("Tuesday.\n");
break;
case 2: printf("Wednesday.\n");
break;
case 3: printf("Thursday.\n");
break;
case 4: printf("Friday.\n");
break;
case 5: printf("Saturday.\n");
break;
case 6: printf("Sunday.\n");
break;
}
return 0;
}
Output 1: Enter a year between 1900 and 2099 1900
The day on 01 January 1900 was Monday.
Output 2: Enter a year between 1900 and 2099 2015
The day on 01 January 2015 was Thursday.
Output 3: Enter a year between 1900 and 2099 2016
The day on 01 January 2016 was Friday.
Output 4: Enter a year between 1900 and 2099 2017
The day on 01 January 2017 was Sunday.
Output 5: Enter a year between 1900 and 2099 2018
The day on 01 January 2018 was Monday.
Lets Use Ternary / Conditional Operator To Find Leap Year
We modify above program and use Ternary Operator or Conditional Operator to find leap year. You can find detailed explanation of finding leap year using Conditional Operator here: C Program To Check Leap Year Using Ternary Operator
Source Code: C Program To Find The Day on 01 January using Gregorian Calendar Using Ternary or Conditional Operator