Even or Odd Number using Ternary Operator: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD, using Ternary / Conditional Operator.

Related Read:
Basic Arithmetic Operations In C
Relational Operators In C

Ternary Operator / Conditional Operator In C

Even or Odd Number: C Program

Even or Odd Number without using Modular Division: C Program

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

C program To check Even or Odd Number using Ternary Operator

 
#include < stdio.h >

int main()
{
    int n;

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

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

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

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.

Even or Odd Number using Ternary Operator: C Program


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

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


Modular division returns remainder of division. For example, 12 / 2 = 6. But 12 % 2 = 0.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is perfectly divisible by 2. If its perfectly divisible by 2, then it’e even number or else its odd 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

Programming Interview / Viva Q&A: 5 (while loop)

In this video tutorial lets understand the while loop a little better. We’ve posted source code of a C program below which has while loop. You need to guess the output of the program.

Related Read:
while loop in C programming

Guess output of the program

 

int main()
{
    int count = 0;

    printf("Start of program\n");

    while(count <= 5);
    {
        printf("Count = %d\n", count);
        count = count + 1;
    }

    printf("End of program\n");

    return 0;
}

Output:
Start of program

Since above C source code has semicolon(;) immediately after while(count <= 5) the control enters infinite loop or indefinite loop, and no other statements after the semicolon gets executed. And the program doesn’t get terminated with return 0.

C Programming Interview / Viva Q&A: 5 (While Loop)


[youtube https://www.youtube.com/watch?v=fqYykTU-jrA]

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


If you remove the semicolon(;) after while(count <= 5) then the program will output numbers from 0 to 5 and the “End of program” statement.

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

while loop in C programming

So far we’ve seen sequential flow of our programs. Next we saw decision control statements like if, if else, else if etc. Now lets learn about loop control statements present in C programming language.

In this video tutorial lets learn about while loop in particular.

Related Read:
Programming Interview / Viva Q&A: 5 (Infinite or Indefinite while loop)

General Syntax of while loop

 
#include < stdio.h >

int main()
{
    while(pre_test_condition)
      statement1;

    return 0;
}

Here while is the keyword. Inside parenthesis we need to write the condition. These conditions must evaluate to Boolean value. i.e., true(non-zero number) or false(0);

The ‘body of while’ loop keeps executing until the condition is true. Control exits while loop once the condition is false.

 
#include < stdio.h >

int main()
{
    while(pre_test_condition)
   {
      statement1;
      statement2;
   }
    return 0;
}

Enclose the statements with curly braces if there are multiple statements within while loop or body of while loop.

while loop in C programming Language


[youtube https://www.youtube.com/watch?v=2Mkc80-uqrs]

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


Example Source Code: while loop

 
#include < stdio.h >

int main()
{
    int count = 1;

    while(count <= 5)
    {
        printf("%d\n", count);
        count = count + 1;
    }

    printf("End of loop\n");

    return 0;
}

Output:
1
2
3
4
5
End of loop

In above C source code, variable count is initialized to 1. Inside while condition we check if variable count is less than or equal to 5. Until variable count is less than or equal to 5, the while loop keeps executing. Inside body of the while loop we increment the value of count by 1 upon each execution of the loop. The condition is checked on execution of the loop. Once variable count is more than 5, the condition becomes false count <= 5 (once count value is 6, the condition becomes false), the execution control exits while loop.

Note: Here the loop execution counter is called ‘loop counter’ or ‘index variable’.

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

Biggest of 3 Numbers: C

Lets write C program to find biggest of 3 numbers, using nested if else statement.

Related Read:
Nested if else Statement In C
Relational Operators In C

Biggest of 3 Numbers Using Ternary Operator: C

C Program To Find Biggest of 3 numbers

 
#include < stdio.h >

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

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

    if( a > b && a > c)
    {
        printf("%d is biggest\n", a);
    }
    else
    {
        if( b > c )
        {
            printf("%d is biggest\n", b);
        }
        else
        {
            printf("%d is biggest\n", c);
        }
    }

    return 0;
}

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

Output 2:
Enter 3 numbers
10
18
15
18 is biggest

Here first we check if a is greater than b. If yes, then we display a as big. Else b or c is biggest. So now we check if b is greater than c, if yes, we display b as biggest else c is biggest. Nested if else condition works great for programs like this.

Biggest of 3 numbers: C Program


[youtube https://www.youtube.com/watch?v=iy–Q0cLnfc]

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


C Program To Find Biggest of 3 numbers

 
#include < stdio.h >

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

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

    if( a > b && a > c)
    {
        big = a;
    }
    else
    {
        if( b > c )
        {
            big = b;
        }
        else
        {
            big = c;
        }
    }

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

    return 0;
}

Output:
Enter 3 numbers
500
550
555
Biggest of 3 number is 555

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