C Program to Calculate Generic Root of a Number using Ternary Operator

Lets write a C program to calculate Generic Root of a Number using Ternary Operator or Conditional Operator.

Related Read:
C Program to Find Generic Root of a Number
Ternary Operator / Conditional Operator In C

For Example: If user input number is 65987, then we add all the individual digits of the number i.e., 6 + 5 + 9 + 8 + 7 = 35. We got 35. Now we add individual digits of number 35 i.e., 3 + 5 = 8. So Generic Root of number 65987 is 8.

Source Code: C Program to Calculate Generic Root of a Number using Ternary Operator

 
#include < stdio.h >

int main()
{
    int num, res;

    printf("Enter a number above 10\n");
    scanf("%d", &num);

    printf("Generic Root of %d is %d\n", num, (res = num % 9) ? res : 9 );

    return 0;
}

Output 1:
Enter a number above 10
65987
Generic Root of 65987 is 8

Output 2:
Enter a number above 10
8
Generic Root of 8 is 8

Output 3:
Enter a number above 10
456
Generic Root of 456 is 6

Output 4:
Enter a number above 10
78910
Generic Root of 78910 is 7

Output 5:
Enter a number above 10
5555
Generic Root of 5555 is 2

C Program to Calculate Generic Root of a Number using Ternary Operator


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

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


Logic To Find Generic Root of a Number using Ternary Operator

(result = num % 9) ? result : 9;

OR

(num % 9) ? (num % 9) : 9;

We divide the user input number by 9 and store the remainder inside variable result. If the number is perfectly divisible by 9 or if the result is zero then we output 9 orelse we output the value present in variable result. Variable result will have the Generic Root of the user entered number. We are applying modular division on the user entered number. And we are dividing it by 9 because 9 is the biggest single digit number and by modulo division of number by 9 we get the Generic Root of the number.

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 Check Leap Year Using Ternary Operator

C Program to Find if a given Year is a Leap Year or Not, using Ternary Operator.

Related Read:
Ternary Operator / Conditional Operator In C
C Program To Check Leap Year

General Form of Ternary Operator

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

We are illustrating the use of multiple nested ternary operator in this program. We have a nested ternary operator in place of expression2 as well as expression3.

Leap Year Logic

Leap Year
1. If a year is a century year(years ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Not Leap Year
1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year.
2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.

C Program To Check Leap Year Using Nested Ternary Operator


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

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


In this video tutorial we illustrate the use of multiple nested ternary operator / conditional operator inside a ternary operator.

First we check if given year is perfectly divisible by 100. If its true, then the entered year is a century year(year ending with 00). If the year is also perfectly divisible by 400, then its leap year, if not, its not a leap year.

If the user entered year is not a century year, then we check if the year is perfectly divisible by 4. If yes, then its a leap year, if not, then its not a leap year.

Check Leap Year Using Ternary Operator: C Program

 
#include < stdio.h >

int main()
{
    int year;

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

    (year % 100 == 0) ?
    ( (year % 400 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    ) :
    ( (year % 4 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    );

    return 0;
}

Output 1:
Enter the year
2019
2019 is not leap year!

Output 2:
Enter the year
2004
2004 is leap year!

Output 3:
Enter the year
2024
2024 is leap year!

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

You can run the program and input any of the above leap year and check for the output.

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

Positive or Negative or Zero Using Ternary Operator: C Program

C Program to check whether the user entered integer number is positive, negative or zero using ternary operator or Conditional operator.

Related Read:
Number is Positive or Negative or Zero: C Program

Check Whether Number Is Positive or Negative or Zero Using Ternary Operator: C Program


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

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


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

Check Whether Number is Positive or Negative or Zero: C Program

#include < stdio.h >

int main()
{
    int a;

    printf("Enter an integer number\n");
    scanf("%d", &a);

    (a > 0) ?
    (printf("%d is positive\n", a)) :
    ( (a < 0) ?
      (printf("%d is Negative\n", a)) :
      (printf("%d is Zero\n", a))  
    );

    return 0;
}

Output 1:
Enter an integer number
15
15 is positive

Output 2:
Enter an integer number
-2
-2 is negative

Output 3:
Enter an integer number
0
0 is zero

In above C source code, we are using nested ternary / conditional operator. First we check if a is greater than 0, if its true then the user entered number is positive. If its false, then we check if a is less than 0 using nested ternary / conditional operator. If that is true, then a is negative, else the user entered number is 0.

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

Even or Odd Number using Ternary Operator and without using Modular Division: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, without using modular division(%) operator and by using Ternary Operator / Conditional Operator.

Related Read:
Even or Odd Number without using Modular Division: C Program
Ternary Operator / Conditional Operator In C

Please visit the links I’ve posted above without fail before watching the video posted below.

Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter a integer number\n");
    scanf("%d", &n);

    ( (n/2)*2 == n ) ?
    (printf("%d is Even\n", n)) :
    (printf("%d is Odd\n", n));


    return 0;
}

Output 1:
Enter a integer number
10
10 is Even

Output 2:
Enter a integer number
5
5 is Odd

Even or Odd Number using Ternary Operator and without using Modular Division: C Program


[youtube https://www.youtube.com/watch?v=4-AoNsgYJQ4]

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


Note: Division of an integer number by 2(which is also an integer number) always returns integer number.

Example 1:
If user enters n = 2;
Applying n = 2 to ( (n/2)*2 == n ).
( (2/2)*2 == 2 )
( (1)*2 == 2 )
( 2 == 2 ) // true

So user entered value, that is, 2 is even number.

Example 2:
If user enters n = 3;
Applying n = 3 to ( (n/2)*2 == n ).
( (3/2)*2 == 3 )
( (1)*2 == 3 )
( 2 == 3 ) // false

2 is not equal to 3. So user entered value, that is, 3 is odd number.
In above example, 3/2 gives back 1 and not 1.5 as 3 is divided by integer which returns only the integer part and discards the decimal part.

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

Biggest of 3 Numbers Using Ternary Operator: C

Lets find biggest of 3 numbers using ternary operator / conditional operator. This program is an example for nested ternary operator.

Related Read:
Ternary Operator / Conditional Operator In C
Logical Operators In C

To find biggest of Three numbers using if-else control structure:
Biggest of 3 Numbers: C

Source Code: Biggest of Three Numbers using ternary operator: C

 
#include < stdio.h >

int main()
{
    int a, b, c, big;

    printf("Enter 3 numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    big = (a>b && a>c) ? (a) : ( (b>c)?(b):(c) );

    printf("Biggest is %d\n", big);

    return 0;
}

Output 1:
Enter 3 numbers
1
2
3
Biggest is 3

Output 2:
Enter 3 numbers
1
3
2
Biggest is 3

Output 1:
Enter 3 numbers
3
2
1
Biggest is 3

In above source code, if a is bigger than b as well as c, then value of a is returned and stored in variable big orelse exression3 gets executed – where we check if b is greater than c.

General Form of Ternary Operator

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

Nested Ternary Operator

In our source code, we nest Ternary Operator inside expression3 to find biggest among b and c.

Note:
if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.

Biggest of 3 Numbers Using Ternary Operator: C Program


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

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


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