Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Random Number Generation, Flow Control, and Conditional Logic in C Programming

Tech 2

Random Number Generation and Formatting

Objective 1: Generate five random numbers with in the range 202400420001 to 202400420100 and output them.

Objective 2: Generate a random integer between 1 and 100.

Objective 3: Format numeric output to display exactly four digits, padding with leading zeros if necesary.

Objective 4: Use the currrent system time as a seed for the random number generator to ensure a different sequence of pseudo-random numbers on each program execution. Omitting this step results in the same sequence being gneerated across multiple runs.

Loop Control and State Manageemnt

Issue 1: In a cumulative purchase system, a customer's transaction amount incorrectly accumulates from the previous customer's total.

Issue 2: Implementing a control flow statement to skip the current iteration of a loop and proceed to the next one.

Traffic Signal Simulator Using Character Input

#include <stdio.h>
int main() {
    char signal_input;
    while (scanf("%c", &signal_input) != EOF) {
        getchar(); // Consume newline character
        if (signal_input == 'r') {
            printf("stop!\n");
        }
        else if (signal_input == 'g') {
            printf("go go go\n");
        }
        else if (signal_input == 'y') {
            printf("wait a minute\n");
        }
        else {
            printf("something must be wrong...\n");
        }
    }
    return 0;
}

Daily Expense Tracker with Statistics

#include<stdio.h>
int main()
{
    float expense, daily_total = 0.0, highest = 0.0, lowest = 20000.0;
    printf("Enter today's expenses. Input -1 to finish:\n");
    while (1) {
        scanf("%f", &expense);
        if (expense == -1) {
            break;
        }
        if (expense > 0 && expense < 20000) {
            daily_total += expense;
            if (expense > highest) {
                highest = expense;
            }
            if (expense < lowest) {
                lowest = expense;
            }
        }
    }
    printf("Total expenses for the day: %.1f\n", daily_total);
    printf("Highest single expense: %.1f\n", highest);
    printf("Lowest single expense: %.1f\n", lowest);
    return 0;
}

Triangle Type Classifier

#include <stdio.h>

int main() {
    int side1, side2, side3;
    while (scanf("%d%d%d", &side1, &side2, &side3) != EOF) {
        if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1) {
            if (side1 == side2 && side2 == side3) {
                printf("Equilateral triangle\n");
            }
            else if (side1 == side2 || side1 == side3 || side2 == side3) {
                printf("Isosceles triangle\n");
            }
            else if (side1 * side1 + side2 * side2 == side3 * side3 || 
                     side1 * side1 + side3 * side3 == side2 * side2 || 
                     side2 * side2 + side3 * side3 == side1 * side1) {
                printf("Right triangle\n");
            }
            else {
                printf("Scalene triangle\n");
            }
        }
        else {
            printf("Not a valid triangle\n");
        }
    }
    return 0;
}

Lucky Day Guessing Game

#include<stdio.h>
#include<stdlib.h>
#include <time.h>

int main() {
    int secret_day, user_guess, attempts = 0;
    srand(time(0));
    secret_day = rand() % 30 + 1;

    printf("Guess your lucky day in April 2026.\n");
    printf("You have 3 attempts. Enter your guess (1~30):");
    while (attempts < 3) {
        scanf("%d", &user_guess);
        attempts++;
        if (user_guess == secret_day) {
            printf("Correct! :)\n");
            break;
        }
        else if (attempts < 3) {
            if (user_guess > secret_day) {
                printf("Your guess is too late. Your lucky day is earlier. Guess again (1~30):");
            }
            else {
                printf("Your guess is too early. Your lucky day is later. Guess again (1~30):");
            }
        }
        else {
            if (user_guess > secret_day) {
                printf("Your guess is too late. Your lucky day is earlier.\n");
            }
            else {
                printf("Your guess is too early. Your lucky day is later.\n");
            }
            printf("No attempts left. Your lucky day in April is the %dth.\n", secret_day);
        }
    }
    return 0;
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.