C Programming Exercises: Random Number Generation, Vending Machine Logic, Traffic Light Control, Expense Tracking, Number Guessing Game, and Pattern Printing
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SAMPLE_COUNT 5
int main() {
srand((unsigned int)time(NULL));
for (int idx = 0; idx < SAMPLE_COUNT; ++idx) {
int random_value = rand() % 100 + 1;
printf("20490042%04d\n", random_value);
}
return 0;
}
This program produces five pseudorandom integers between 1 and 100 inclusive. Each is formatted as a zero-padded four-digit suffix appended to the prefix 20490042, yielding values in the range 204900420001 to 204900420100.
#include <stdio.h>
int main() {
int selection;
int units;
float balance = 0.0f;
float tendered;
float difference;
while (1) {
printf("\nBeverage Dispenser Menu:\n");
printf("1. Cola — ¥3.00 per unit\n");
printf("2. Sprite — ¥3.00 per unit\n");
printf("3. Orange Juice — ¥5.00 per unit\n");
printf("4. Mineral Water — ¥2.00 per unit\n");
printf("0. Exit transaction\n");
printf("Select item: ");
scanf("%d", &selection);
if (selection == 0) break;
if (selection < 1 || selection > 4) {
printf("Invalid selection. Try again.\n");
continue;
}
printf("Quantity: ");
scanf("%d", &units);
if (units < 0) {
printf("Quantity must be non-negative.\n");
continue;
}
switch (selection) {
case 1:
case 2:
balance += 3.0f * units;
break;
case 3:
balance += 5.0f * units;
break;
case 4:
balance += 2.0f * units;
break;
}
printf("Amount tendered: ¥");
scanf("%f", &tendered);
difference = tendered - balance;
printf("Subtotal: ¥%.2f\n", balance);
printf("Change due: ¥%.2f\n", difference);
balance = 0.0f;
}
printf("Thank you for shopping!\n");
return 0;
}
The vending machine simulator accumulates cost per selection using integer-based quantity input and floating-point pricing. Invalid inputs trigger appropriate feedback, and break terminates the loop on exit request while continue skips invalid iterations without exiting.
#include <stdio.h>
int main() {
char signal;
printf("Enter traffic light color (g/y/r): ");
scanf(" %c", &signal);
switch (signal) {
case 'g':
case 'G':
printf("Proceed.\n");
break;
case 'y':
case 'Y':
printf("Prepare to stop.\n");
break;
case 'r':
case 'R':
printf("Stop immediately.\n");
break;
default:
printf("Unrecognized signal. Verify input.\n");
break;
}
return 0;
}
This implementation handles both uppercase and lowercase input for robustness and includes a fallback message for unrecognized characters.
#include <stdio.h>
int main() {
double entry;
double total = 0.0;
double highest = 0.0;
double lowest = 0.0;
double average;
int tally = 0;
printf("Enter daily expenses (−1 to finish):\n");
while (1) {
scanf("%lf", &entry);
if (entry == -1.0) break;
if (tally == 0) {
highest = entry;
lowest = entry;
} else {
if (entry > highest) highest = entry;
if (entry < lowest) lowest = entry;
}
total += entry;
++tally;
}
if (tally > 0) {
average = total / tally;
printf("Total spending: ¥%.1f\n", total);
printf("Highest single expense: ¥%.1f\n", highest);
printf("Lowest single expense: ¥%.1f\n", lowest);
printf("Average per transaction: ¥%.1f\n", average);
} else {
printf("No entries recorded.\n");
}
return 0;
}
The expense tracker initializes bounds only after the first valid entry, avoiding assumptions about initial values. It also guards against division-by-zero when no data is entered.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int target, guess;
const int max_tries = 3;
srand((unsigned int)time(NULL));
target = rand() % 30 + 1;
printf("Guess your lucky day in April 2025 (1–30)\n");
printf("You have %d attempts. Begin:\n", max_tries);
for (int attempt = 0; attempt < max_tries; ++attempt) {
printf("Attempt %d/%d: ", attempt + 1, max_tries);
scanf("%d", &guess);
if (guess == target) {
printf("Congratulations! You guessed correctly!\n");
return 0;
} else if (guess < target) {
printf("Too early — your lucky day is later.\n");
} else {
printf("Too late — your lucky day is earlier.\n");
}
}
printf("All attempts exhausted. Your lucky day is %d.\n", target);
return 0;
}
The guessing game uses clear attempt counting and contextual hints based on numeric comparison, with early termination upon success.
#include <stdio.h>
int main() {
int height;
printf("Enter number of rows for inverted triangle: ");
scanf("%d", &height);
for (int row = height; row >= 1; --row) {
// Leading spaces
for (int pad = 0; pad < height - row; ++pad) {
printf(" ");
}
// First line: '0'
for (int col = 0; col < 2 * row - 1; ++col) {
printf(" 0 ");
}
printf("\n");
// Second line: '<H>'
for (int pad = 0; pad < height - row; ++pad) {
printf(" ");
}
for (int col = 0; col < 2 * row - 1; ++col) {
printf("<H> ");
}
printf("\n");
// Third line: 'I I'
for (int pad = 0; pad < height - row; ++pad) {
printf(" ");
}
for (int col = 0; col < 2 * row - 1; ++col) {
printf("I I ");
}
printf("\n");
}
return 0;
}
The pattern printer constructs a inverted triangular layout with three distinct symbol rows per level (0, <H>, I I) and consistent horizontal spacing.