C Programming Laboratory Exercises

Source code:

#include <stdio.h>
int main()
{
    printf(" o    o\n");
    printf("<H>  <H>\n");
    printf("I I  I I\n");
    
    return 0;
}

Output:

Pattern output

Task 2: Triangle Validation

Source code:

#include <stdio.h>

int main()
{
    double side1, side2, side3;

    // Input three side lengths
    scanf_s("%lf%lf%lf", &side1, &side2, &side3);

    // Check if sides can form a triangle
    if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1)
        printf("Can form a triangle\n");
    else
        printf("Cannot form a triangle\n");

    return 0;
}

Output examples:

Triangle validation example 1

Triangle validation example 2

Task 3: User Input Evaluation

Source code:

#include <stdio.h>
int main()
{
    char response1, response2;  // Variables to store user answers

    printf("Have you been preparing before class and reviewing after? (Enter y/Y for yes, n/N for no): ");
    response1 = getchar(); // Get character input from keyboard

    getchar(); // This line consumes the newline character from previous input

    printf("\nHave you been practicing by writing code? (Enter y/Y for yes, n/N for no): ");
    response2 = getchar();

    if ((response1 == 'y' || response1 == 'Y') && (response2 == 'y' || response2 == 'Y'))
        printf("\nRome wasn't built in a day, keep up the good work:)\n");
    else
        printf("\nRome wasn't destroyed in a day, let's start building\n");

    return 0;
}

Output examples:

User input evaluation example 1

User input evaluation example 2

Task 4: Input/Output Operations

Source code:

#include<stdio.h>
int main()
{
    double num1, num2;
    char char1, char2, char3;
    int int1, int2, int3;
    
    scanf("%d%d%d", &int1, &int2, &int3);
    printf("int1 = %d, int2 = %d, int3 = %d\n", int1, int2, int3);
    
    scanf(" %c %c %c", &char1, &char2, &char3);
    printf("char1 = %c, char2 = %c, char3 = %c\n", char1, char2, char3);
    
    scanf_s("%lf%lf", &num1, &num2);
    printf("num1=%f, num2=%lf\n", num1, num2);
    
    return 0;
}

Output:

Input/output operations example

Task 5: Time Conversion

Source code:

// Calculate how many years are in a given number of seconds
#include<stdio.h>
int main()
{
    long long seconds;
    double years;
    scanf("%lld", &seconds);
    
    years = (double)seconds / (60 * 60 * 24 * 365);
    printf("%lld seconds equals approximately %f years\n", seconds, years);
    return 0;
}

Output:

Time conversion example

Task 6: Power Calculation

Source code:

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

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

Task 7: Temperature Conversion

Source code:

#include<stdio.h>

int main()
{
    double celsius, fahrenheit;
    while(scanf("%lf", &celsius) != EOF)
    {
        fahrenheit = 1.8 * celsius + 32;
        printf("When Celsius is %lf, the corresponding Fahrenheit is %lf\n", celsius, fahrenheit);
    }
    return 0;
}

Output:

Temperature conversion example

Task 8: Triengle Area Calculation

Source code:

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

int main()
{
    int a, b, c;
    float semiPerimeter, area;
    double input;
    
    while(scanf("%lf", &input) != EOF)
    {
        printf("Enter the three sides of the triangle: ");
        scanf("%d %d %d", &a, &b, &c);
        semiPerimeter = (a + b + c) / 2.0;
        area = (float)sqrt(semiPerimeter * (semiPerimeter - a) * (semiPerimeter - b) * (semiPerimeter - c));
        printf("a=%d, b=%d, c=%d, area of triangle=%.3f\n", a, b, c, area);
    }
    
    return 0;
}

Output:

Triangle area calculation example

Tags: c programming Input/Output conditional logic mathematical operations temperature conversion

Posted on Fri, 03 Jul 2026 17:40:21 +0000 by thines