luc031.c (1542B)
1 /* Write a program to enter numbers till the user wants. At the end it 2 should display the count of positive, negative and zeros entered. */ 3 /* Let Us C, Chap- 5, Page - 87, Qn No.: B(d) */ 4 5 #include <stdio.h> 6 int main() 7 { 8 int choice = 1, num, positive_count = 0, negative_count = 0, zero_count = 0; 9 while (choice == 1) 10 { 11 printf("\nEnter the number (Type any character and press Enter to finish.) : "); 12 choice = scanf("%d", &num); // Checking whether the user has input any characters 13 if (choice == 1) 14 { 15 printf("Number recorded : %d", num); 16 if (num < 0) 17 negative_count++; 18 else if (num > 0) 19 positive_count++; 20 else if (num == 0) 21 zero_count++; 22 } 23 else 24 { 25 // If the user inputs any characters, then choice = 0, it means he doesn't want to give any more input; 26 choice = 0; 27 printf("\nCharacter received. Stopping input...\n"); 28 } 29 } 30 // Display the final results 31 printf("\n====================================\n"); 32 printf(" Analysis Complete\n"); 33 printf("====================================\n"); 34 printf("Positive numbers entered: %d\n", positive_count); 35 printf("Negative numbers entered: %d\n", negative_count); 36 printf("Zeroes entered: %d\n", zero_count); 37 printf("Total numbers recorded: %d\n", positive_count + negative_count + zero_count); 38 printf("====================================\n"); 39 }