Skip to content

🖥️ CS111 — Introduction to Programming Lab

  • The Programming Lab (CS111) provides hands-on experience in coding, helping students implement algorithms, develop programs, and strengthen computational thinking through practical exercises.
Lab BadgeLanguage BadgePrograms Badge

Basic C programming exercises for beginners


Click any program below to view the complete source code

🎯 Basic Operations

1. add.c · Addition of Two Numbers
c
#include <stdio.h>

int main(void) {
    int a = 2, b = 3;
    printf("%d + %d = %d\n", a, b, a + b);
    return 0;
}
32. sum.c · Sum of Two Numbers
c
#include <stdio.h>

int main(void) {
    int a = 1, b = 2;
    printf("Sum: %d\n", a + b);
    return 0;
}
24. mul.c · Multiplication
c
#include <stdio.h>

int main(void) {
    int a = 6, b = 7;
    printf("%d * %d = %d\n", a, b, a * b);
    return 0;
}
17. division.c · Division Operation
c
#include <stdio.h>

int main(void) {
    double a = 10.0, b = 3.0;
    printf("%.2f / %.2f = %.2f\n", a, b, a / b);
    return 0;
}
14. dif1.c · Difference Method 1
c
#include <stdio.h>

int main(void) {
    int a = 10, b = 3;
    int diff = a - b;
    printf("Difference: %d\n", diff);
    return 0;
}
15. dif2.c · Difference Method 2 (Absolute)
c
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int a = 5, b = 12;
    int diff = abs(a - b);
    printf("Absolute difference: %d\n", diff);
    return 0;
}
20. hello.c · Hello World
c
#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");
    return 0;
}

📐 Geometric Calculations

2. areac.c · Area of Circle
c
#include <stdio.h>

int main(void) {
    double r = 1.5;
    printf("Area of circle (r=%.2f): %.4f\n", r, 3.14159 * r * r);
    return 0;
}
3. areaR.c · Area of Rectangle
c
#include <stdio.h>

int main(void) {
    double length = 5.0, width = 3.0;
    double area = length * width;
    printf("Area of rectangle: %.2f\n", area);
    return 0;
}
4. areaRec.c · Area of Rectangle (User Input)
c
#include <stdio.h>

int main(void) {
    double length, width;
    printf("Enter length: ");
    scanf("%lf", &length);
    printf("Enter width: ");
    scanf("%lf", &width);
    printf("Area: %.2f\n", length * width);
    return 0;
}
23. milage.c · Compute Mileage
c
#include <stdio.h>

int main(void) {
    double distance = 150.0, fuel = 10.0;
    printf("Mileage: %.2f km/l\n", distance / fuel);
    return 0;
}

🧮 Calculators

6. Calc.c · Simple Calculator
c
#include <stdio.h>

int main(void) {
    double a, b;
    char op;
    printf("Enter operation (a op b): ");
    scanf("%lf %c %lf", &a, &op, &b);

    switch(op) {
        case '+': printf("%.2f\n", a + b); break;
        case '-': printf("%.2f\n", a - b); break;
        case '*': printf("%.2f\n", a * b); break;
        case '/': printf("%.2f\n", a / b); break;
        default: printf("Invalid operator\n");
    }
    return 0;
}
7. Calculator.c · Enhanced Calculator
c
#include <stdio.h>

int main(void) {
    double num1, num2, result;
    char operator;

    printf("Enter first number: ");
    scanf("%lf", &num1);
    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);
    printf("Enter second number: ");
    scanf("%lf", &num2);

    switch(operator) {
        case '+':
            result = num1 + num2;
            printf("%.2f + %.2f = %.2f\n", num1, num2, result);
            break;
        case '-':
            result = num1 - num2;
            printf("%.2f - %.2f = %.2f\n", num1, num2, result);
            break;
        case '*':
            result = num1 * num2;
            printf("%.2f * %.2f = %.2f\n", num1, num2, result);
            break;
        case '/':
            if(num2 != 0) {
                result = num1 / num2;
                printf("%.2f / %.2f = %.2f\n", num1, num2, result);
            } else {
                printf("Error: Division by zero\n");
            }
            break;
        default:
            printf("Invalid operator\n");
    }
    return 0;
}

🔢 Number Operations

16. DigitSum.c · Sum of Digits
c
#include <stdio.h>

int main(void) {
    int n = 12345, sum = 0;
    while(n) {
        sum += n % 10;
        n /= 10;
    }
    printf("Digit sum: %d\n", sum);
    return 0;
}
12. DecToBin.c · Decimal to Binary Conversion
c
#include <stdio.h>

void dec_to_bin(unsigned n) {
    if(n > 1) dec_to_bin(n / 2);
    printf("%u", n % 2);
}

int main(void) {
    unsigned n = 13;
    printf("%u in binary: ", n);
    dec_to_bin(n);
    printf("\n");
    return 0;
}
5. bitOE.c · Check Odd or Even (Bitwise)
c
#include <stdio.h>

int main(void) {
    int n = 7;
    if(n & 1)
        printf("%d is odd\n", n);
    else
        printf("%d is even\n", n);
    return 0;
}
26. Powof2.c · Check Power of Two
c
#include <stdio.h>

int main(void) {
    int n = 16;
    int ok = (n > 0) && ((n & (n - 1)) == 0);
    printf("%d is %spower of two\n", n, ok ? "" : "not ");
    return 0;
}
18. FibboSeries.c · Fibonacci Series
c
#include <stdio.h>

int main(void) {
    int n = 10, a = 0, b = 1, next;

    printf("Fibonacci Series: %d %d ", a, b);
    for(int i = 2; i < n; i++) {
        next = a + b;
        printf("%d ", next);
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}
27. Prime.c · Check Prime Number
c
#include <stdio.h>
#include <math.h>

int main(void) {
    int n = 17, ok = 1;
    if(n <= 1) ok = 0;
    else {
        for(int i = 2; i <= sqrt(n); i++)
            if(n % i == 0) {
                ok = 0;
                break;
            }
    }
    printf("%d is %sprime\n", n, ok ? "" : "not ");
    return 0;
}
11. Dcprime.c · Check Prime (Optimized)
c
#include <stdio.h>

int main(void) {
    int n = 29, isPrime = 1;

    if(n <= 1) isPrime = 0;
    else if(n == 2) isPrime = 1;
    else if(n % 2 == 0) isPrime = 0;
    else {
        for(int i = 3; i * i <= n; i += 2) {
            if(n % i == 0) {
                isPrime = 0;
                break;
            }
        }
    }

    printf("%d is %sprime\n", n, isPrime ? "" : "not ");
    return 0;
}
28. Primenum.c · Print Prime Numbers
c
#include <stdio.h>
#include <math.h>

int main(void) {
    int limit = 20;
    printf("Prime numbers up to %d: ", limit);

    for(int n = 2; n <= limit; n++) {
        int ok = 1;
        for(int i = 2; i <= sqrt(n); i++)
            if(n % i == 0) {
                ok = 0;
                break;
            }
        if(ok) printf("%d ", n);
    }
    printf("\n");
    return 0;
}

📝 String Operations

29. Reverse.c · Reverse a String
c
#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "hello";
    printf("Original: %s\n", s);
    printf("Reversed: ");
    for(int i = strlen(s) - 1; i >= 0; i--)
        putchar(s[i]);
    putchar('\n');
    return 0;
}
8. caseconvert.c · Convert to Uppercase
c
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char s[] = "Hello World";
    for(int i = 0; s[i]; i++)
        s[i] = toupper((unsigned char)s[i]);
    printf("%s\n", s);
    return 0;
}
9. caseconvert1.c · Convert to Lowercase
c
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char s[] = "HELLO WORLD";
    for(int i = 0; s[i]; i++)
        s[i] = tolower((unsigned char)s[i]);
    printf("%s\n", s);
    return 0;
}
10. caseconvert2.c · Toggle Case
c
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char s[] = "HeLLo WoRLd";
    for(int i = 0; s[i]; i++) {
        if(isupper(s[i]))
            s[i] = tolower(s[i]);
        else if(islower(s[i]))
            s[i] = toupper(s[i]);
    }
    printf("%s\n", s);
    return 0;
}
31. StrLen.c · String Length
c
#include <stdio.h>

int main(void) {
    char str[] = "Hello World";
    int len = 0;

    while(str[len] != '\0')
        len++;

    printf("Length of '%s': %d\n", str, len);
    return 0;
}
25. Palindrome.c · Check Palindrome
c
#include <stdio.h>
#include <string.h>

int main(void) {
    char str[] = "madam";
    int len = strlen(str);
    int isPalindrome = 1;

    for(int i = 0; i < len / 2; i++) {
        if(str[i] != str[len - 1 - i]) {
            isPalindrome = 0;
            break;
        }
    }

    printf("%s is %sa palindrome\n", str, isPalindrome ? "" : "not ");
    return 0;
}

🔄 Comparison & Swap

21. Largest2.c · Largest of Two Numbers
c
#include <stdio.h>

int main(void) {
    int a = 10, b = 20;
    int largest = (a > b) ? a : b;
    printf("Largest: %d\n", largest);
    return 0;
}
22. Largest3.c · Largest of Three Numbers
c
#include <stdio.h>

int main(void) {
    int a = 10, b = 25, c = 15;
    int largest = a;

    if(b > largest) largest = b;
    if(c > largest) largest = c;

    printf("Largest: %d\n", largest);
    return 0;
}
33. swap1.c · Swap Using Temp Variable
c
#include <stdio.h>

int main(void) {
    int a = 5, b = 10, temp;

    printf("Before swap: a = %d, b = %d\n", a, b);
    temp = a;
    a = b;
    b = temp;
    printf("After swap: a = %d, b = %d\n", a, b);

    return 0;
}
34. Swap2.c · Swap Without Temp Variable
c
#include <stdio.h>

int main(void) {
    int a = 5, b = 10;

    printf("Before swap: a = %d, b = %d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("After swap: a = %d, b = %d\n", a, b);

    return 0;
}

🍬 Practice Problems

36. SweetDistribute.c · Distribute Sweets
c
#include <stdio.h>

int main(void) {
    int sweets = 10, kids = 3;
    printf("Each gets %d sweets\n", sweets / kids);
    return 0;
}
35. sweet.c · Sweet Distribution (Detailed)
c
#include <stdio.h>

int main(void) {
    int sweets = 20, kids = 4;
    printf("Total sweets: %d\n", sweets);
    printf("Number of kids: %d\n", kids);
    printf("Each kid gets: %d sweets\n", sweets / kids);
    printf("Remaining: %d sweets\n", sweets % kids);
    return 0;
}
37. sweets.c · Sweets Calculation
c
#include <stdio.h>

int main(void) {
    int total = 15, eaten = 3, remaining;
    remaining = total - eaten;
    printf("Total sweets: %d\n", total);
    printf("Eaten: %d\n", eaten);
    printf("Remaining: %d\n", remaining);
    return 0;
}

🔧 Miscellaneous

19. header_files.c · Header Files Example
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(void) {
    printf("Header files example\n");
    printf("Using stdio.h for input/output\n");
    printf("Using stdlib.h for utilities\n");
    printf("Using string.h for string operations\n");
    printf("Using math.h for mathematical functions\n");
    return 0;
}
13. des.c · Descriptive Example
c
#include <stdio.h>

int main(void) {
    printf("This is a descriptive program example\n");
    printf("It demonstrates basic C syntax\n");
    return 0;
}
30. s.c · Simple Program
c
#include <stdio.h>

int main(void) {
    printf("Simple test program\n");
    return 0;
}
38. test.c · Test Program
c
#include <stdio.h>

int main(void) {
    printf("This is a test program\n");
    printf("Testing basic C operations\n");
    return 0;
}
39. wsemi.c · Semicolon Example
c
#include <stdio.h>

int main(void) {
    int x = 5;
    int y = 10;
    int sum = x + y;
    printf("Sum of %d and %d is %d\n", x, y, sum);
    return 0;
}

CS Preparation Notes