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

Number is Positive or Negative or Zero: C Program

C Program to check whether the user entered integer number is positive, negative or zero using else if construct.

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


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

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


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);

    if(a > 0)
    {
        printf("%d is positive\n", a);
    }
    else if(a < 0)
    {
        printf("%d is negative\n", a);
    }
    else
    {
        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

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

Operator Precedence / Priority: C

Often times we use arithmetic operations, relational operators, logical operators and assignment operators together in a instruction / statement. We must know the precedence / priority of the operator so that we can write proper code.

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

Operator Hierarchy / Precedence / Priority

PriorityOperatorType
1!Logical NOT Operator
2* / %Arithmetic Operator
3+ –Arithmetic Operator
4< > <= >=Relational Operator
5== !=Relational Operator
6&&Logical AND Operator
7||Logical OR Operator
8=Assignment Operator

Note: Write down above table of operator priority on a piece of paper and have it handy while writing the code. You’ll get used to it after some time – until then, keep revisiting the hierarchy of operators.

Operator Precedence / Priority / Hierarchy: C


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

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

Post-decrement and Pre-decrement Operator: C Program

In this video tutorial we show the differences and working of post-decrement and pre-decrement operators.

Note: In pre-decrement, first the value of the variable is decremented after that the assignment or other operations are carried. In post-decrement, first assignment or other operations occur, after that the value of the variable is decremented.

Note:
a- -;
– -a;
a = a – 1;
a -= 1;

are all same if used independently.

Post-decrement and Pre-decrement Operator: C Program


[youtube https://www.youtube.com/watch?v=eaGXQ-GgLPw]

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


Pre-decrement Operator: C Program

#include < stdio.h >

int main()
{
    int a = 10, b;

    b = --a;

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

    return 0;
}

Output:
b = 9
a = 9

Here first the value of a decrements and then is assigned to variable b. So both a and b value will be 9.

#include < stdio.h >

int main()
{
    int a = 10;

    printf("a = %d\n\n", --a);
    printf("a = %d\n", a);

    return 0;
}

Output:
a = 9
a = 9

Here in the first printf statement a value gets decremented and then printed out.

Post-decrement Operator: C Program

#include < stdio.h >

int main()
{
    int a = 10, b;

    b = a--;

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

    return 0;
}

Output:
b = 10
a = 9

Here first value of a(i.e., 10) is assigned to b and then value of a is decremented. So b = 10 and a = 9 is printed.

#include < stdio.h >

int main()
{
    int a = 10;

    printf("a = %d\n\n", a--);
    printf("a = %d\n", a);

    return 0;
}

Output:
a = 10
a = 9

Here in the first printf statement a value gets printed after that its value gets decremented, which is shown in second printf 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

Post-increment and Pre-increment Operator: C Program

In this video tutorial we show the differences and working of post-increment and pre-increment operators.

Note: In pre-increment, first the value of the variable is incremented after that the assignment or other operations are carried. In post-increment, first assignment or other operations occur, after that the value of the variable is incremented.

Post-increment and Pre-increment Operator: C Program


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

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


Pre-increment Operator: C Program

#include < stdio.h >

int main()
{
    int a = 10, b;

    b = ++a;

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

    return 0;
}

Output:
b = 11
a = 11

Here first the value of a increments and then is assigned to variable b. So both a and b value will be 11.

#include < stdio.h >

int main()
{
    int a = 10;

    printf("a = %d\n\n", ++a);
    printf("a = %d\n", a);

    return 0;
}

Output:
a = 11
a = 11

Here in the first printf statement a value gets incremented and then printed out.

Post-increment Operator: C Program

#include < stdio.h >

int main()
{
    int a = 10, b;

    b = a++;

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

    return 0;
}

Output:
b = 10
a = 11

Here first value of a(i.e., 10) is assigned to b and then value of a is incremented. So b = 10 and a = 11 is printed.

#include < stdio.h >

int main()
{
    int a = 10;

    printf("a = %d\n\n", a++);
    printf("a = %d\n", a);

    return 0;
}

Output:
a = 10
a = 11

Here in the first printf statement a value gets printed after that its value gets incremented, which is shown in second printf 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