C Program To Determine Designation of Employee Based On Salary

Rewrite the following C program using Conditional / Ternary Operator.

Source Code: C Program To Determine Designation of Employee Based On Salary Using nested if else condition

#include<stdio.h>

int main()
{
    float sal;

    printf("Enter the salary\n");
    scanf("%f", &sal);

    if(sal >= 25000 && sal <= 40000)
    {
        printf("Manager\n");
    }
    else
        if(sal >= 15000 && sal < 25000)
            printf("Accountant\n");
        else
            printf("Clerk\n");

    return 0;
}

Note: We need to rewrite the above C program using Ternary / Conditional Operator.

Good Example of Nested Ternary Operator
C Program To Check Leap Year Using Ternary Operator

Related Read:
Nested if else Statement In C
Ternary Operator / Conditional Operator In C
Relational Operators In C
Logical Operators In C

Ternary / Conditional Operator General Form

(expression_1) ? (expression_2) : (expression_3);

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


[youtube https://www.youtube.com/watch?v=W04xqRxFTHk]

YouTube Link: https://www.youtube.com/watch?v=W04xqRxFTHk [Watch the Video In Full Screen.]

Source Code: C Program To Determine Designation of Employee Based On Salary using Conditional Operator

#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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Special Symbol or Not using Conditional Operator

Using Conditional / Ternary Operator determine, whether a character entered through the keyboard is a Special Symbol or not.

Note: ASCII values start from 0 to 255. So we have total of 266 ASCII values.

ASCII Code for Special Symbols
0 – 47
58 – 64
91 – 96
123 – 255

Also Check:
C Program To Find Lowercase Alphabet or Not using Conditional Operator

Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code

Ternary / Conditional Operator General Form

(expression_1) ? (expression_2) : (expression_3);

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


[youtube https://www.youtube.com/watch?v=LDxdmrCRqCU]

YouTube Link: https://www.youtube.com/watch?v=LDxdmrCRqCU [Watch the Video In Full Screen.]

Source Code: C Program To Find Special Symbol or Not using Conditional Operator

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    ( (ch >= 0  && ch <= 47) ||
      (ch >= 58 && ch <= 64) ||
      (ch >= 91 && ch <= 96) ||
      (ch >= 123) ) ?
      printf("Character Entered Is a Special Symbol\n") :
      printf("Character Entered Is not a Special Symbol\n");

    return 0;
}

Output 1:
Enter a Character
#
Character Entered Is a Special Symbol

Output 2:
Enter a Character
a
Character Entered Is not a Special Symbol

Output 3:
Enter a Character
Z
Character Entered Is not a Special Symbol

Output 4:
Enter a Character
5
Character Entered Is not a Special Symbol

Output 5:
Enter a Character
$
Character Entered Is a Special Symbol

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Lowercase Alphabet or Not using Conditional Operator

Using Conditional Operator / Ternary Operator determine, whether the character entered through the keyboard is a lower case English alphabet or not.

Also Check:
C Program To Find Special Symbol or Not using Conditional Operator

Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a character
a

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


[youtube https://www.youtube.com/watch?v=YM50IowuYhk]

YouTube Link: https://www.youtube.com/watch?v=YM50IowuYhk [Watch the Video In Full Screen.]

Source Code: C Program To Find Lowercase Alphabet or Not using Conditional Operator

#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

ASCII Values of Lowercase English Alphabets

ASCII value of a is 97

ASCII value of b is 98

ASCII value of c is 99

ASCII value of d is 100

ASCII value of e is 101

ASCII value of f is 102

ASCII value of g is 103

ASCII value of h is 104

ASCII value of i is 105

ASCII value of j is 106

ASCII value of k is 107

ASCII value of l is 108

ASCII value of m is 109

ASCII value of n is 110

ASCII value of o is 111

ASCII value of p is 112

ASCII value of q is 113

ASCII value of r is 114

ASCII value of s is 115

ASCII value of t is 116

ASCII value of u is 117

ASCII value of v is 118

ASCII value of w is 119

ASCII value of x is 120

ASCII value of y is 121

ASCII value of z is 122

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array

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.

Related Read:
while loop in C programming
Positive or Negative or Zero Using Ternary Operator: C Program

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

scale

Expected Output for the Input

User Input:
Enter the limit
6
Enter 6 numbers
-1
0
2
-3
5
6

Output:
Positive Numbers: 3
Negative Numbers: 2
Number of zero: 1

Logic To Count Positive, Negative and Zero using Ternary Operator and without using Array

Complete logic to this program is present at C Program To Count Positive, Negative and Zero without using Array

Video Tutorial: C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array


[youtube https://www.youtube.com/watch?v=OMGSxVCav24]

YouTube Link: https://www.youtube.com/watch?v=OMGSxVCav24 [Watch the Video In Full Screen.]

Source Code: C Program to Count Positive, Negative and Zero using Ternary Operator and without using Array

#include < stdio.h >

int main()
{
    int limit, num, positive = 0, negative = 0, zero = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);

    while(limit)
    {
        scanf("%d", &num);
        (num > 0) ? positive++ : ((num < 0) ? negative++ : zero ++);
        limit--;
    }

    printf("\nPositive Numbers: %d\n", positive);
    printf("Negative Numbers: %d\n", negative);
    printf("Number of zero: %d\n", zero);

    return 0;
}

Output:
Enter the limit
5
Enter 5 numbers
2
-1
5
6
0

Positive Numbers: 3
Negative Numbers: 1
Number of zero: 1

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find The Day on 01 January using Gregorian Calendar

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.

Related Read
while loop in C programming
C Program To Check Leap Year
Simple Calculator Program using Switch Case: C

Gregorian Calendar: the calendar introduced in 1582 by Pope Gregory XIII, as a modification of the Julian Calendar.

Expected Output for the Input

User Input:
Enter a year between 1900 and 2099
2020

Output:
The day on 01 January 2020 was Wednesday.

Video Tutorial: C Program To Find The Day on 01 January in Gregorian Calendar


[youtube https://www.youtube.com/watch?v=mG2gfYAX14Q]

YouTube Link: https://www.youtube.com/watch?v=mG2gfYAX14Q [Watch the Video In Full Screen.]

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:

0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday

We make use of Switch case to match the number in variable day to the day of the week.

Source Code: C Program To Find The Day on 01 January using Gregorian Calendar

#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

#include < stdio.h >

int main()
{
    int ref_year = 1900, year, leap = 0, nonleap = 0, total_days = 0, day = 0;

    printf("Enter a year between 1900 and 2099\n");
    scanf("%d", &year);

    while(ref_year < year)
    {
        (ref_year % 100 == 0) ?
        ( (ref_year % 400 == 0)?
          (leap++):
          (nonleap++)
        ) :
        ( (ref_year % 4 == 0)?
          (leap++):
          (nonleap++)
        );

        ref_year++;
    }

    total_days = nonleap * 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
2019

The day on 01 January 2019 was Tuesday.

Output 2:
Enter a year between 1900 and 2099
2020

The day on 01 January 2020 was Wednesday.

Output 3:
Enter a year between 1900 and 2099
2021

The day on 01 January 2021 was Friday.

Output 4:
Enter a year between 1900 and 2099
2022

The day on 01 January 2022 was Saturday.

Output 5:
Enter a year between 1900 and 2099
2023

The day on 01 January 2023 was Sunday.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert