bsc

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

commit 3daef3921999e64e12c06e6fdf17724ea2bc1d92
parent cfccdaf1eaae4e85d23662ea8ebe66be47023e2a
Author: Amit Dutta <amitdutta4255@gmail.com>
Date:   Wed,  8 Oct 2025 12:11:24 +0530

new 31 with logic

Diffstat:
Aletusc/luc031-logic.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aletusc/luc031.c | 41+++++++++++++++++++++++++++++++++++++++++
2 files changed, 142 insertions(+), 0 deletions(-)

diff --git a/letusc/luc031-logic.c b/letusc/luc031-logic.c @@ -0,0 +1,100 @@ +/* Write a program to enter numbers till the user wants. At the end it +should display the count of positive, negative and zeros entered. */ +/* Author - Amit Dutta, Date - 8th OCT, 2025 */ +/* Let Us C, Chap- 5, Page - 87, Qn No.: B(d) */ + +/* + * DETAILED PROGRAM PLAN: + * 1. INITIALIZATION: Set three counters (positive_count, negative_count, zero_count) to zero. + * 2. INPUT LOOP: Start an infinite loop to continuously request user input. + * 3. READ INPUT: Use fgets() to read the user's input as a generic string (char array). + * This allows the program to accept multi-digit numbers (e.g., "-500") or the command ('n'). + * 4. TERMINATION CHECK: Immediately check if the input string is exactly "n" using strcmp(). + * If true, the user wants to quit, so break the loop. + * 5. VALIDATION & CONVERSION: If the input is not "n", use sscanf() to safely attempt to convert + * the string into an integer. + * 6. COUNTING LOGIC: If sscanf() successfully reads an integer (sscanf returns 1): + * - If the number is > 0, increment positive_count. + * - If the number is < 0, increment negative_count. + * - If the number is == 0, increment zero_count. + * 7. ERROR HANDLING: If the input is neither "n" nor a valid number (sscanf returns 0), + * inform the user of invalid input and continue the loop. + * 8. FINAL DISPLAY: After the loop terminates, print the final totals for positive, negative, and zero counts. + */ + +#include <stdio.h> +#include <stdlib.h> // for strtol +#include <string.h> // for strcmp + +// Maximum size of the input line +#define MAX_INPUT_LEN 15 + +int main() { + // Initialize counters + int positive_count = 0; + int negative_count = 0; + int zero_count = 0; + + // Buffer to store the user's input as a string (e.g., "123", "-50", or "n") + char input_buffer[MAX_INPUT_LEN]; + int number; + + printf("--- Number Analyzer ---\n"); + printf("Enter numbers one by one. Type 'n' and press Enter to finish.\n\n"); + + // Loop until the user enters 'n' + while (1) { + printf("Enter number or 'n': "); + + // Read the entire line of input into the buffer + if (fgets(input_buffer, MAX_INPUT_LEN, stdin) == NULL) { + // Handle EOF (end of file) or reading error + break; + } + + // Remove the trailing newline character from the input_buffer + // The last character will be '\n' if the input was shorter than MAX_INPUT_LEN + size_t len = strlen(input_buffer); + if (len > 0 && input_buffer[len - 1] == '\n') { + input_buffer[len - 1] = '\0'; + } + + // 1. Check for the termination condition + if (strcmp(input_buffer, "n") == 0) { + printf("\n'n' received. Stopping input...\n"); + break; // Exit the while loop + } + + // 2. Attempt to convert the input string to an integer + // sscanf attempts to read the string according to the format "%d" (decimal integer) + // It returns 1 if a number was successfully read. + int conversions = sscanf(input_buffer, "%d", &number); + + if (conversions == 1) { + // Conversion was successful, now check the number's sign + if (number > 0) { + positive_count++; + } else if (number < 0) { + negative_count++; + } else { + zero_count++; + } + printf(" -> Number recorded: %d\n", number); + } else { + // Conversion failed. The input was neither 'n' nor a valid integer. + printf(" -> Invalid input. Please enter a valid number or 'n'.\n"); + } + } + + // Display the final results + printf("\n====================================\n"); + printf(" Analysis Complete\n"); + printf("====================================\n"); + printf("Positive numbers entered: %d\n", positive_count); + printf("Negative numbers entered: %d\n", negative_count); + printf("Zeroes entered: %d\n", zero_count); + printf("Total numbers recorded: %d\n", positive_count + negative_count + zero_count); + printf("====================================\n"); + + return 0; +}+ \ No newline at end of file diff --git a/letusc/luc031.c b/letusc/luc031.c @@ -0,0 +1,40 @@ +/* Write a program to enter numbers till the user wants. At the end it +should display the count of positive, negative and zeros entered. */ +/* Author - Amit Dutta, Date - 8th OCT, 2025 */ +/* Let Us C, Chap- 5, Page - 87, Qn No.: B(d) */ + +#include <stdio.h> +int main() +{ + int choice = 1, num, positive_count = 0, negative_count = 0, zero_count = 0; + while (choice == 1) + { + printf("\nEnter the number (Type any character and press Enter to finish.) : "); + choice = scanf("%d", &num); // Checking whether the user has input any characters + if (choice == 1) + { + printf("Number recorded : %d", num); + if (num < 0) + negative_count++; + else if (num > 0) + positive_count++; + else if (num == 0) + zero_count++; + } + else + { + // If the user inputs any characters, then choice = 0, it means he doesn't want to give any more input; + choice = 0; + printf("\nCharacter received. Stopping input...\n"); + } + } + // Display the final results + printf("\n====================================\n"); + printf(" Analysis Complete\n"); + printf("====================================\n"); + printf("Positive numbers entered: %d\n", positive_count); + printf("Negative numbers entered: %d\n", negative_count); + printf("Zeroes entered: %d\n", zero_count); + printf("Total numbers recorded: %d\n", positive_count + negative_count + zero_count); + printf("====================================\n"); +}+ \ No newline at end of file
© 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