bsc

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

luc031-logic.c (4267B)


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