C Programming Exercises: Loops, Input Handling, and Mathematical Computations
Task 1.1: Repeated ASCII Art Output
A while loop prints a simple stick-figure pattern twice:
#include <stdio.h>
int main() {
int counter = 0;
while (counter < 2) {
printf(" O \n");
printf("<H>\n");
printf("I I\n");
++counter;
}
return 0;
}
Task 1.2: Side-by-Side Pattern Rendering
This version displays two identical figures aligned horizontally:
#include <stdio.h>
int main() {
printf(" O O \n");
printf("<H> <H>\n");
printf("I I I I\n");
return 0;
}
Task 2: Triangle Validitty Checker
Validates whether three real numbers can form a triangle using the triangle inequality theorem:
#include <stdio.h>
int main() {
double side_a, side_b, side_c;
scanf("%lf %lf %lf", &side_a, &side_b, &side_c);
if (side_a + side_b > side_c &&
side_b + side_c > side_a &&
side_a + side_c > side_b) {
printf("Valid triangle\n");
} else {
printf("Invalid triangle\n");
}
return 0;
}
Task 3: Interactive Habit Assessment
Reads two single-character responses and evaluates consistency in affirmative input (y or Y). The extra getchar() consumes the newline left by the first input to prevent skipping the second prompt:
#include <stdio.h>
int main() {
char response1, response2;
printf("Did you review before class and after? (y/Y or n/N): ");
response1 = getchar();
getchar(); // Discard newline
printf("\nDid you practice coding? (y/Y or n/N): ");
response2 = getchar();
if ((response1 == 'y' || response1 == 'Y') &&
(response2 == 'y' || response2 == 'Y')) {
printf("\nConsistency pays off — keep going!\n");
} else {
printf("\nLet’s build better habits together.\n");
}
return 0;
}
Task 4: Mixed-Type Input Parsing
Demonstrates sequential reading of integers, characters, and floating-point values, with leading whitespace handling in format strings:
#include <stdio.h>
int main() {
int val1, val2, val3;
char ch1, ch2, ch3;
double num1, num2;
scanf("%d %d %d", &val1, &val2, &val3);
printf("val1 = %d, val2 = %d, val3 = %d\n", val1, val2, val3);
scanf(" %c %c %c", &ch1, &ch2, &ch3);
printf("ch1 = %c, ch2 = %c, ch3 = %c\n", ch1, ch2, ch3);
scanf(" %lf %lf", &num1, &num2);
printf("num1 = %.2f, num2 = %.2f\n", num1, num2);
return 0;
}
Task 5: Time Unit Conversion
Computes approximate years in one billion seconds using integer division:
#include <stdio.h>
int main() {
const int SECONDS_PER_YEAR = 31536000;
int years = 1000000000 / SECONDS_PER_YEAR;
printf("One billion seconds ≈ %d years\n", years);
return 0;
}
Task 6.1: Exponential Growth Simulation
Repeatedly reads a base value and computes its 365th power. Note: This may overflow for |x| > 1.
#include <stdio.h>
#include <math.h>
int main() {
double base;
while (scanf("%lf", &base) == 1) {
double result = pow(base, 365);
printf("%.2f^365 = %.2f\n", base, result);
}
return 0;
}
Task 7: Celsius-to-Fahrenheit Converter
Processes multiple temperature inputs until end-of-file:
#include <stdio.h>
int main() {
double celsius;
while (scanf("%lf", &celsius) != EOF) {
double fahrenheit = 1.8 * celsius + 32.0;
printf("Celsius = %.2f → Fahrenheit = %.2f\n", celsius, fahrenheit);
}
return 0;
}
Task 8: Triangle Area Calculator Using Heron’s Formula
Computes area from side lengths for multiple test cases:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
while (scanf("%lf %lf %lf", &a, &b, &c) == 3) {
double semi = (a + b + c) / 2.0;
double area = sqrt(semi * (semi - a) * (semi - b) * (semi - c));
printf("a=%.3f, b=%.3f, c=%.3f → area=%.3f\n", a, b, c, area);
}
return 0;
}