bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc031-logic.c

luc031-logic.c (4076B)


      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 /*
      6  * DETAILED PROGRAM PLAN:
      7  * 1. INITIALIZATION: Set three counters (positive_count, negative_count, zero_count) to zero.
      8  * 2. INPUT LOOP: Start an infinite loop to continuously request user input.
      9  * 3. READ INPUT: Use fgets() to read the user's input as a generic string (char array). 
     10  * This allows the program to accept multi-digit numbers (e.g., "-500") or the command ('n').
     11  * 4. TERMINATION CHECK: Immediately check if the input string is exactly "n" using strcmp(). 
     12  * If true, the user wants to quit, so break the loop.
     13  * 5. VALIDATION & CONVERSION: If the input is not "n", use sscanf() to safely attempt to convert 
     14  * the string into an integer.
     15  * 6. COUNTING LOGIC: If sscanf() successfully reads an integer (sscanf returns 1):
     16  * - If the number is > 0, increment positive_count.
     17  * - If the number is < 0, increment negative_count.
     18  * - If the number is == 0, increment zero_count.
     19  * 7. ERROR HANDLING: If the input is neither "n" nor a valid number (sscanf returns 0), 
     20  * inform the user of invalid input and continue the loop.
     21  * 8. FINAL DISPLAY: After the loop terminates, print the final totals for positive, negative, and zero counts.
     22  */
     23 
     24 #include <stdio.h>
     25 #include <stdlib.h> // for strtol
     26 #include <string.h> // for strcmp
     27 
     28 // Maximum size of the input line
     29 #define MAX_INPUT_LEN 15 
     30 
     31 int main() {
     32     // Initialize counters
     33     int positive_count = 0;
     34     int negative_count = 0;
     35     int zero_count = 0;
     36 
     37     // Buffer to store the user's input as a string (e.g., "123", "-50", or "n")
     38     char input_buffer[MAX_INPUT_LEN];
     39     int number;
     40 
     41     printf("--- Number Analyzer ---\n");
     42     printf("Enter numbers one by one. Type 'n' and press Enter to finish.\n\n");
     43 
     44     // Loop until the user enters 'n'
     45     while (1) {
     46         printf("Enter number or 'n': ");
     47 
     48         // Read the entire line of input into the buffer
     49         if (fgets(input_buffer, MAX_INPUT_LEN, stdin) == NULL) {
     50             // Handle EOF (end of file) or reading error
     51             break;
     52         }
     53 
     54         // Remove the trailing newline character from the input_buffer
     55         // The last character will be '\n' if the input was shorter than MAX_INPUT_LEN
     56         size_t len = strlen(input_buffer);
     57         if (len > 0 && input_buffer[len - 1] == '\n') {
     58             input_buffer[len - 1] = '\0';
     59         }
     60 
     61         // 1. Check for the termination condition
     62         if (strcmp(input_buffer, "n") == 0) {
     63             printf("\n'n' received. Stopping input...\n");
     64             break; // Exit the while loop
     65         }
     66 
     67         // 2. Attempt to convert the input string to an integer
     68         // sscanf attempts to read the string according to the format "%d" (decimal integer)
     69         // It returns 1 if a number was successfully read.
     70         int conversions = sscanf(input_buffer, "%d", &number);
     71 
     72         if (conversions == 1) {
     73             // Conversion was successful, now check the number's sign
     74             if (number > 0) {
     75                 positive_count++;
     76             } else if (number < 0) {
     77                 negative_count++;
     78             } else {
     79                 zero_count++;
     80             }
     81             printf("  -> Number recorded: %d\n", number);
     82         } else {
     83             // Conversion failed. The input was neither 'n' nor a valid integer.
     84             printf("  -> Invalid input. Please enter a valid number or 'n'.\n");
     85         }
     86     }
     87 
     88     // Display the final results
     89     printf("\n====================================\n");
     90     printf("   Analysis Complete\n");
     91     printf("====================================\n");
     92     printf("Positive numbers entered: %d\n", positive_count);
     93     printf("Negative numbers entered: %d\n", negative_count);
     94     printf("Zeroes entered:           %d\n", zero_count);
     95     printf("Total numbers recorded:   %d\n", positive_count + negative_count + zero_count);
     96     printf("====================================\n");
     97 
     98     return 0;
     99 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror