Biggest of Two Numbers Using Ternary Operator: C

Lets find biggest of 2 numbers using ternary operator / conditional operator.

Related Read:
Ternary Operator / Conditional Operator In C

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

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

 
#include < stdio.h >

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

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

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

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
5
6
Biggest of 5 and 6 is 6

Output 2:
Enter 2 numbers
40
15
Biggest of 40 and 15 is 40

 
#include < stdio.h >

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

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

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

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
500
900
Biggest of 500 and 900 is 900

In above source code, if a is bigger than b, then value of a is returned and stored in variable big orelse value of variable b is stored in variable big.

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.

Biggest of 2 Numbers Using Ternary Operator: C


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

YouTube Link: https://www.youtube.com/watch?v=TB11OWMyiR8 [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

Ternary Operator / Conditional Operator In C

In this video tutorial we will show you how to use Conditional Operator. They are also called Ternary Operators as they take 3 arguments.

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.

Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.

C Program: Ternary Operator / Conditional Operator

 
#include < stdio.h >

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

    // (expression1) ? (expression2) : (expression3);

     (a+b) ? (c = 10) : (c = 20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 10

a+b is 2 which is non-zero, so it is considered as true. So c = 10 gets executed.

 
#include < stdio.h >

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

    // (expression1) ? (expression2) : (expression3);

     (a-b) ? (c = 10) : (c = 20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 20

a-b is 0 which is zero, so it is considered as false. So c = 20 gets executed.

 
#include < stdio.h >

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

    // (expression1) ? (expression2) : (expression3);

    c = (a+b) ? (10) : (20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 10

 
#include < stdio.h >

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

    // (expression1) ? (expression2) : (expression3);

    c = (a-b) ? (10) : (20);

    printf("Value of C is %d\n", c);

    return 0;
}

Output:
Value of C is 20

Ternary Operator / Conditional Operator In C


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

YouTube Link: https://www.youtube.com/watch?v=wubWvdcjMcw [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

C Programming Interview / Viva Q&A: 4 (Logical NOT Operator)

In this video tutorial lets put our understanding of logical NOT(!) operator to test.

Related Read:
Logical Operators In C
Relational Operators In C

Logical NOT(!) Operator in C Program

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30, c;

    if( !(a > b) )
        b = 50;
    c = 70;

    printf("b = %d and c = %d\n", b, c);

    return 0;
}

In this c source code, a is 20 and b is 30. Inside if condition we check if a is greater than b. But a is less than b, so it returns false(0). But we’ve a Logical NOT operator(!) surrounding the false result. So it converts it to true. Hence the next line of code that is, b = 50 gets executed.

So the output is: b = 50 and c = 70

C Programming Interview / Viva Q&A: 4 (Logical NOT Operator)


[youtube https://www.youtube.com/watch?v=gRl9-zZMqm8]

YouTube Link: https://www.youtube.com/watch?v=gRl9-zZMqm8 [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 Two Numbers: C

In this video tutorial we ask the user to enter 2 integer numbers and using if else and relational operator we check and display the biggest of 2 numbers on the console.

Related Read:
if else statement in C
Relational Operators In C

C Program To Find Biggest of 2 numbers

 
#include < stdio.h >

int main()
{
    int a, b;

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

    if(a > b)
    {
        printf("Biggest of %d and %d is %d\n", a, b, a);
    }
    else
    {
        printf("Biggest of %d and %d is %d\n", a, b, b);
    }

    return 0;
}

Output 1:
Enter 2 numbers
20
30
Biggest of 20 and 30 is 30

Output 2:
Enter 2 numbers
200
300
Biggest of 200 and 300 is 300

Biggest of 2 numbers: C Program


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

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


C Program To Find Biggest of 2 numbers

 
#include < stdio.h >

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

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

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

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output:
Enter 2 numbers
75
100
Biggest of 75 and 100 is 100

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 Programming Interview / Viva Q&A: 3 (Logical Operator)

In this C program we’ve 2 operands and one AND operator(&&). We use Logical NOT Operator to change the true and false conditions inside if statement. Check the source code below and guess the output.

Related Read:
Logical Operators In C

Logical && Operator in C Program

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30;

    if( a && b )
    {
        printf("a = %d\n", a);
    }
    else
    {
        printf("b = %d\n", b);
    }
    return 0;
}

Output:
a = 20

In above C program, a = 20 and b = 30. In C, any non-zero number is considered as true. So the condition is if statement returns true and the block of code inside if statement gets executed.

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30;

    if(  !(!a) && a  )
    {
        printf("a = %d\n", a);
    }
    else
    {
        printf("b = %d\n", b);
    }
    return 0;
}

Lets evaluate the if condition in above c source code. a = 20, which is non-zero number, so its considered as true(1). So negating the true(1) value !a gives false(0). Negating that false(0) value !(!a) gives true(1).

So !(!a) returns true(1). So both sides of &&(AND) is true, so the whole thing returns true. So the block of code inside if condition gets executed.

Output:
a = 20

C Programming Interview / Viva Q&A: 3 (Logical Operator)


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

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


Important:
1. Logical &&(AND) Operator returns true when both sides operands have true(non-zero number) value. In any other case it returns false(0) value.
2. Any non-zero number is considered as true and zero(0) is considered as false in C programming language.

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