Ques:-16) Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.

Below is a C program that allows the user to enter numbers until they choose to stop. It then displays the count of positive numbers, negative numbers, and zeros entered.

				
					#include<stdio.h>
#include<conio.h>
void main()
{
int x[],pos,neg,zero,i,n;
clrscr();
printf("How many number\n");
scanf("%d",&n);
printf("Enter all the numbers in the list\n");
pos=0;
neg=0;
zero=0;
for(i=0;i<n;i++)
{
scanf("\n%d",&x[i]);
}
for(i=0;i<n;i++)
{
if(x[i]>0)
pos++;
if(x[i]<0)
neg++;
if(x[i]==0)
zero++;
}
printf("Count of positive number is:%d\n",pos);
printf("Count of negative number is:%d\n",neg);
printf("Count of zeros is:%d\n",zero);
getch();
}

				
			

Out Put

Another Example

				
					#include <stdio.h>

int main() {
    int num;
    int positiveCount = 0, negativeCount = 0, zeroCount = 0;
    char choice;

    do {
        printf("Enter a number: ");
        scanf("%d", &num);

        if(num > 0) {
            positiveCount++;
        } else if(num < 0) {
            negativeCount++;
        } else {
            zeroCount++;
        }

        printf("Do you want to enter another number? (y/n): ");
        scanf(" %c", &choice); // Note the space before %c to consume any newline character left in the input buffer
    } while(choice == 'y' || choice == 'Y');

    printf("\nCount of positive numbers: %d\n", positiveCount);
    printf("Count of negative numbers: %d\n", negativeCount);
    printf("Count of zeros: %d\n", zeroCount);

    return 0;
}

				
			

Explanation:

  1. Variable Declaration:

    • num: Stores the current number entered by the user.
    • positiveCount, negativeCount, zeroCount: Counters for positive numbers, negative numbers, and zeros respectively.
    • choice: Stores the user’s decision to continue entering numbers or not.
  2. Input Loop:

    • The do-while loop allows the user to enter numbers until they choose to stop.
    • After entering a number, the program checks if the number is positive, negative, or zero and increments the respective counter.
    • The user is then prompted to enter another number or stop. If they enter ‘y’ or ‘Y’, the loop continues.
  3. Output:

    • After the loop exits, the program prints the counts of positive numbers, negative numbers, and zeros.

Sample Output:

				
					Enter a number: 5
Do you want to enter another number? (y/n): y
Enter a number: -3
Do you want to enter another number? (y/n): y
Enter a number: 0
Do you want to enter another number? (y/n): y
Enter a number: 2
Do you want to enter another number? (y/n): n

Count of positive numbers: 2
Count of negative numbers: 1
Count of zeros: 1

				
			

You can compile and run this program using a C compiler to see the results.

Scroll to Top