Program: 100
Write a c program to enter numbers till the user wants. At the end it should display the count of positive, negative and zeros entered.
#include<stdio.h> #include<conio.h> int main() { int i, num, count_p=0, count_n=0, count_z=0; int arr[100]; //size of array printf("Enter Numbers: "); scanf("%d", &num); //take input from user for "num" numbers for(i=0;i<num;i++) { scanf("%d", &arr[i]); } //count the numbers for(i=0;i<num;i++) { //check for positive numbers if(arr[i]>0) { count_p++; } else if(arr[i]<0) { count_n++; } else if(arr[i]==0) { count_z++; } else { printf("Wrong Entry"); break; } } printf("Positive Numbers: %d\n", count_p); printf("Negative Numbers: %d\n", count_n); printf("Zero Numbers: %d\n", count_z); }
Output:
Enter Numbers: 5-1 2 -4 -5 0
Positive Numbers: 1
Negative Numbers: 3
Zero Numbers: 1
Second Method:
#include<stdio.h> #include<conio.h> int main() { int number, positive = 0, negative = 0, zero = 0; char choice='Y'; do { printf("Enter a number: "); scanf("%d", &number); if (number > 0) { positive++; } else if (number < 0) { negative++; } else { zero++; } printf("Do you want to Continue(y/n)? "); scanf(" %c", &choice); //note the space before " %c" otherwise your while loop would not work }while(choice == 'y' || choice == 'Y'); printf("\nPositive Numbers :%d\nNegative Numbers :%d\nZero Numbers :%d", positive, negative, zero); }
Output:
Enter a number: -4
Do you want to Continue(y/n)? y
Enter a number: 0
Do you want to Continue(y/n)? y
Enter a number: 7
Do you want to Continue(y/n)? y
Enter a number: -45
Do you want to Continue(y/n)? y
Enter a number: 12
Do you want to Continue(y/n)? n
Positive Numbers :2
Negative Numbers :2
Zero Numbers :1
Leave a Comment