Basic C Programming Exercises

Task 1: ASCII Art Stick Figure

Print a stick figure vertically:

#include <stdio.h>

int main() {
    printf(" O \n");
    printf("/|\\\n");
    printf(" | \n");
    return 0;
}

Print two stick figures vertically:

#include <stdio.h>

int main() {
    printf(" O \n");
    printf("/|\\\n");
    printf(" | \n");
    printf(" O \n");
    printf("/|\\\n");
    printf(" | \n");
    return 0;
}

Print two stick figures horizontally:

#include <stdio.h>

int main() {
    printf(" O \t O \n");
    printf("/|\\\t/|\\\n");
    printf(" | \t | \n");
    return 0;
}

Task 2: Triangle Validity Check

Determine if three side lengths can form a triangle.

#include <stdio.h>

int main() {
    double edge1, edge2, edge3;
    scanf("%lf %lf %lf", &edge1, &edge2, &edge3);

    if (edge1 + edge2 > edge3 && edge1 + edge3 > edge2 && edge2 + edge3 > edge1) {
        printf("Valid triangle\n");
    } else {
        printf("Invalid triangle\n");
    }
    return 0;
}

Task 3: User Confirmation Dialog

Prompt for two responses and validate them.

#include <stdio.h>

int main() {
    char first_response, second_response;
    printf("Did you review before class? (y/Y for yes, n/N for no): ");
    first_response = getchar();
    getchar(); // consume newline

    printf("\nDid you practice coding? (y/Y for yes, n/N for no): ");
    second_response = getchar();

    if ((first_response == 'y' || first_response == 'Y') && 
        (second_response == 'y' || second_response == 'Y')) {
        printf("\nRome wasn't built in a day. Keep it up!\n");
    } else {
        printf("\nRome wasn't destroyed in a day. Let's build it.\n");
    }
    return 0;
}

Task 4: Input Parsing Correction

Read integers, characters, and floats correctly.

#include <stdio.h>

int main() {
    double x_coord, y_coord;
    char char_one, char_two, char_three;
    int num1, num2, num3;

    scanf("%d %d %d", &num1, &num2, &num3);
    printf("num1 = %d, num2 = %d, num3 = %d\n", num1, num2, num3);

    scanf(" %c %c %c", &char_one, &char_two, &char_three);
    printf("char_one = %c, char_two = %c, char_three = %c\n", char_one, char_two, char_three);

    scanf("%lf %lf", &x_coord, &y_coord);
    printf("x_coord = %f, y_coord = %f\n", x_coord, y_coord);
    return 0;
}

Task 5: Seconds to Years Conversion

Convert 1 billion seconds to years, rounded.

#include <stdio.h>
#include <math.h>

int main() {
    long long total_sec = 1000000000LL;
    double sec_per_year = 60.0 * 60 * 24 * 365;
    int years = (int)round(total_sec / sec_per_year);
    printf("One billion seconds ≈ %d years\n", years);
    return 0;
}

Task 6: Exponential Growth Calculation

Compute a number raised to the 365th power.

#include <stdio.h>
#include <math.h>

int main() {
    double base, result;
    scanf("%lf", &base);
    result = pow(base, 365);
    printf("%.2f^365 = %.2f\n", base, result);
    return 0;
}

Task 7: Temperature Conversion

Convert Celsius to Fahrenheit with repeated input.

#include <stdio.h>

int main() {
    double celsius, fahr;
    while (scanf("%lf", &celsius) == 1) {
        fahr = celsius * 9.0 / 5.0 + 32;
        printf("Celsius: %.2f, Fahrenheit: %.2f\n", celsius, fahr);
    }
    return 0;
}

Task 8: Triangle Area Calculation

Compute area from three side lengths, repeated input.

#include <stdio.h>
#include <math.h>

int main() {
    double a, b, c, s, area;
    while (scanf("%lf %lf %lf", &a, &b, &c) == 3) {
        s = (a + b + c) / 2.0;
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        printf("Sides: %.2f, %.2f, %.2f. Area: %.3f\n", a, b, c, area);
    }
    return 0;
}

Tags: c programming Beginner exercises Console I/O arithmetic

Posted on Wed, 20 May 2026 07:47:21 +0000 by OldWolf