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

Leave a Reply

Your email address will not be published. Required fields are marked *