C Programming

Solved Example

Practice Questions

Exercise Question

Count positive, negative and zeros

Objective

Write a program to count positive, negative, and zero entered. The program should ask numbers from the user until the user wants.

Program / Solution

#include<stdio.h>
#include<ctype.h>

int main() {
	char ch;
	int n,pos_count,neg_count,zero_count;
	
	pos_count = neg_count = zero_count = 0;
	
	do {
		printf("\nEnter a number: ");
		scanf("%d",&n);
		
		if(n > 0) {
			pos_count++;
		}else if(n < 0) {
			neg_count++;
		}else {
			zero_count++;
		}
		
		printf("Do you want to continue(Y/N): ");
		ch = getche();
		
		ch = tolower(ch);
	}while(ch == 'y');
	
	printf("\nPositive Count = %d\n",pos_count);
	printf("Negative Count = %d\n",neg_count);
	printf("Zero Count = %d\n",zero_count);
	
	return 0;
}

Sample Output

Enter a number: 5
Do you want to continue(Y/N): y
Enter a number: -665
Do you want to continue(Y/N): y
Enter a number: 65
Do you want to continue(Y/N): y
Enter a number: 88
Do you want to continue(Y/N): y
Enter a number: 0
Do you want to continue(Y/N): y
Enter a number: -98
Do you want to continue(Y/N): n
Positive Count = 3
Negative Count = 2
Zero Count = 1