Basic C Programming Exercises and Code Examples

Task 1: Displaying Patterns

The following program displays a simple character pattern:


#include <stdio.h>

int main() {
    printf(" O \n");
    printf("<H>\n");
    printf("I I\n");
    printf(" O \n");
    printf("<H>\n");
    printf("I I\n");
    return 0;
}

Another version aligns two patterns horizontally using tab characters:


#include <stdio.h>

int main() {
    printf(" O ");
    printf("\t");
    printf(" O \n");
    printf("<H>");
    printf("\t");
    printf("<H>\n");
    printf("I I");
    printf("\t");
    printf("I I\n");
    return 0;
}

Task 2: Triangle Validity Check

This program checks if three input values can form a valid triangle:


#include <stdio.h>

int main() {
    double a, b, c;
    scanf("%lf%lf%lf", &a, &b, &c);
    if (a + b > c && a + c > b && b + c > a)
        printf("Valid triangle\n");
    else
        printf("Invalid triangle\n");
    return 0;
}

Task 3: Conditional Feedback Based on User Input

This program evaluates two user responses and provides feedabck:


#include <stdio.h>

int main() {
    char ans1, ans2;
    printf("Did you review lessons before and after class? (y/Y or n/N): ");
    ans1 = getchar();

    char flush = getchar(); // Clear newline from buffer

    printf("\nDid you practice coding? (y/Y or n/N): ");
    ans2 = getchar();

    if ((ans1 == 'y' || ans1 == 'Y') && (ans2 == 'y' || ans2 == 'Y'))
        printf("\nGreat work! Keep it up :)\n");
    else
        printf("\nIt's never too late to start\n");

    return 0;
}

Task 4: Input Handling and Debugging

This program demonstrates correct input hadnling and format specifiers:


#include <stdio.h>

int main() {
    double x, y;
    char c1, c2, c3;
    int a1, a2, a3;

    scanf("%d%d%d", &a1, &a2, &a3);
    printf("a1 = %d, a2 = %d, a3 = %d\n", a1, a2, a3);

    scanf(" %c %c %c", &c1, &c2, &c3); // Space before %c to skip whitespace
    printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3);

    scanf("%lf%lf", &x, &y);
    printf("x = %lf, y = %lf\n", x, y);

    return 0;
}

Task 5: Time Conversion

This program converts one billion seconds into years:


#include <stdio.h>

int main() {
    int year;
    year = (1e9) / (60.0 * 60 * 24 * 365) + 0.5;
    printf("One billion seconds equals approximately %d years\n", year);
    return 0;
}

Task 6: Exponential Calculation

This program calculates the 365th power of an input number:


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

int main() {
    double x, result;
    while (scanf("%lf", &x) != EOF) {
        result = pow(x, 365);
        printf("%.2f raised to 365: %.2f\n\n", x, result);
    }
    return 0;
}

Task 7: Temperature Conversion

This program converts Celsius to Fahrenheit:


#include <stdio.h>

int main() {
    double celsius, fahrenheit;
    while (scanf("%lf", &celsius) != EOF) {
        fahrenheit = (9.0 / 5) * celsius + 32;
        printf("Celsius: %.2f -> Fahrenheit: %.2f\n\n", celsius, fahrenheit);
    }
    return 0;
}

Task 8: Triangle Area Calculation

This program calculates the area of a triangle using Heron’s formula:


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

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

Tags: C programming Input Handling math functions conditional logic

Posted on Fri, 26 Jun 2026 17:12:13 +0000 by Huuggee