Lesson 58 of 60 – C Practical Programs
97%

C Practical Programs

Practical programming helps you understand how C concepts are used to solve real problems. In this lesson, we will practice common beginner and intermediate C programs using variables, operators, conditions, loops, arrays, strings, functions, and pointers.

Note: Try to write each program yourself first. Then compare your solution with the example and test it with different inputs.

1. Print Hello World

The simplest C program prints a message on the screen.

#include <stdio.h>

int main()
{
    printf("Hello World!");

    return 0;
}

2. Add Two Numbers

#include <stdio.h>

int main()
{
    int a, b, sum;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    sum = a + b;

    printf("Sum = %d", sum);

    return 0;
}

3. Find Even or Odd

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if(number % 2 == 0)
    {
        printf("Even number");
    }
    else
    {
        printf("Odd number");
    }

    return 0;
}

The remainder operator % is useful for checking divisibility by 2.

4. Check Positive, Negative, or Zero

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if(number > 0)
    {
        printf("Positive");
    }
    else if(number < 0)
    {
        printf("Negative");
    }
    else
    {
        printf("Zero");
    }

    return 0;
}

5. Find the Largest of Two Numbers

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    if(a > b)
    {
        printf("%d is largest", a);
    }
    else if(b > a)
    {
        printf("%d is largest", b);
    }
    else
    {
        printf("Both are equal");
    }

    return 0;
}

6. Find the Largest of Three Numbers

#include <stdio.h>

int main()
{
    int a, b, c, largest;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &a, &b, &c);

    largest = a;

    if(b > largest)
        largest = b;

    if(c > largest)
        largest = c;

    printf("Largest = %d", largest);

    return 0;
}

7. Calculate Simple Interest

Formula: SI = (P × R × T) / 100

#include <stdio.h>

int main()
{
    float p, r, t, si;

    printf("Enter principal, rate and time: ");
    scanf("%f %f %f", &p, &r, &t);

    si = (p * r * t) / 100;

    printf("Simple Interest = %.2f", si);

    return 0;
}

8. Swap Two Numbers

#include <stdio.h>

int main()
{
    int a, b, temp;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    temp = a;
    a = b;
    b = temp;

    printf("After swapping: %d %d", a, b);

    return 0;
}

9. Check Leap Year

#include <stdio.h>

int main()
{
    int year;

    printf("Enter year: ");
    scanf("%d", &year);

    if((year % 400 == 0) ||
       (year % 4 == 0 && year % 100 != 0))
    {
        printf("Leap year");
    }
    else
    {
        printf("Not a leap year");
    }

    return 0;
}

10. Print Numbers from 1 to 10

#include <stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 10; i++)
    {
        printf("%d ", i);
    }

    return 0;
}

11. Print Multiplication Table

#include <stdio.h>

int main()
{
    int number, i;

    printf("Enter a number: ");
    scanf("%d", &number);

    for(i = 1; i <= 10; i++)
    {
        printf("%d x %d = %d\n",
               number, i, number * i);
    }

    return 0;
}

12. Find Sum of First N Numbers

#include <stdio.h>

int main()
{
    int n, i, sum = 0;

    printf("Enter n: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++)
    {
        sum += i;
    }

    printf("Sum = %d", sum);

    return 0;
}

13. Find Factorial of a Number

#include <stdio.h>

int main()
{
    int n, i;
    long long factorial = 1;

    printf("Enter a number: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++)
    {
        factorial *= i;
    }

    printf("Factorial = %lld", factorial);

    return 0;
}

14. Check Prime Number

#include <stdio.h>

int main()
{
    int n, i, prime = 1;

    printf("Enter a number: ");
    scanf("%d", &n);

    if(n < 2)
    {
        prime = 0;
    }

    for(i = 2; i * i <= n; i++)
    {
        if(n % i == 0)
        {
            prime = 0;
            break;
        }
    }

    if(prime)
        printf("Prime number");
    else
        printf("Not a prime number");

    return 0;
}

15. Reverse a Number

#include <stdio.h>

int main()
{
    int n, reverse = 0, digit;

    printf("Enter a number: ");
    scanf("%d", &n);

    while(n != 0)
    {
        digit = n % 10;
        reverse = reverse * 10 + digit;
        n /= 10;
    }

    printf("Reverse = %d", reverse);

    return 0;
}

16. Check Palindrome Number

#include <stdio.h>

int main()
{
    int n, original, reverse = 0, digit;

    printf("Enter a number: ");
    scanf("%d", &n);

    original = n;

    while(n != 0)
    {
        digit = n % 10;
        reverse = reverse * 10 + digit;
        n /= 10;
    }

    if(original == reverse)
        printf("Palindrome number");
    else
        printf("Not a palindrome");

    return 0;
}

17. Find Sum of Digits

#include <stdio.h>

int main()
{
    int n, sum = 0, digit;

    printf("Enter a number: ");
    scanf("%d", &n);

    while(n != 0)
    {
        digit = n % 10;
        sum += digit;
        n /= 10;
    }

    printf("Sum of digits = %d", sum);

    return 0;
}

18. Fibonacci Series

#include <stdio.h>

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

    printf("Enter number of terms: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++)
    {
        printf("%d ", a);

        next = a + b;
        a = b;
        b = next;
    }

    return 0;
}

19. Find Largest Element in an Array

#include <stdio.h>

int main()
{
    int numbers[5];
    int i, largest;

    printf("Enter 5 numbers:\n");

    for(i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
    }

    largest = numbers[0];

    for(i = 1; i < 5; i++)
    {
        if(numbers[i] > largest)
        {
            largest = numbers[i];
        }
    }

    printf("Largest = %d", largest);

    return 0;
}

20. Calculate Array Sum and Average

#include <stdio.h>

int main()
{
    int numbers[5];
    int i, sum = 0;
    float average;

    printf("Enter 5 numbers:\n");

    for(i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
        sum += numbers[i];
    }

    average = (float)sum / 5;

    printf("Sum = %d\n", sum);
    printf("Average = %.2f", average);

    return 0;
}

21. Count Vowels in a String

#include <stdio.h>
#include <ctype.h>

int main()
{
    char text[100];
    int i, count = 0;

    printf("Enter a string: ");
    fgets(text, sizeof(text), stdin);

    for(i = 0; text[i] != '\0'; i++)
    {
        char ch = tolower((unsigned char)text[i]);

        if(ch == 'a' || ch == 'e' ||
           ch == 'i' || ch == 'o' ||
           ch == 'u')
        {
            count++;
        }
    }

    printf("Vowels = %d", count);

    return 0;
}

22. Find String Length

#include <stdio.h>
#include <string.h>

int main()
{
    char text[100];

    printf("Enter a string: ");
    fgets(text, sizeof(text), stdin);

    printf("Length = %zu", strlen(text));

    return 0;
}

The strlen() function counts characters before the terminating null character.

23. Reverse a String

#include <stdio.h>
#include <string.h>

int main()
{
    char text[100];
    int i;

    printf("Enter a string: ");
    fgets(text, sizeof(text), stdin);

    int length = strlen(text);

    if(length > 0 && text[length - 1] == '\n')
    {
        text[--length] = '\0';
    }

    printf("Reverse = ");

    for(i = length - 1; i >= 0; i--)
    {
        putchar(text[i]);
    }

    return 0;
}

24. Function to Find Maximum

#include <stdio.h>

int maximum(int a, int b)
{
    if(a > b)
        return a;

    return b;
}

int main()
{
    int x, y;

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    printf("Maximum = %d", maximum(x, y));

    return 0;
}

Using functions helps divide a program into smaller reusable parts.

25. Swap Numbers Using Pointers

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = *a;

    *a = *b;
    *b = temp;
}

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    swap(&a, &b);

    printf("After swapping: %d %d", a, b);

    return 0;
}

26. Student Structure Program

#include <stdio.h>

struct Student
{
    int id;
    char name[50];
    float marks;
};

int main()
{
    struct Student student;

    printf("Enter student ID: ");
    scanf("%d", &student.id);

    printf("Enter name: ");
    scanf("%49s", student.name);

    printf("Enter marks: ");
    scanf("%f", &student.marks);

    printf("\nStudent Details\n");
    printf("ID: %d\n", student.id);
    printf("Name: %s\n", student.name);
    printf("Marks: %.2f\n", student.marks);

    return 0;
}

27. File Write Program

#include <stdio.h>

int main()
{
    FILE *file;

    file = fopen("data.txt", "w");

    if(file == NULL)
    {
        printf("Unable to open file");
        return 1;
    }

    fprintf(file, "Welcome to C programming!");

    fclose(file);

    printf("Data written successfully");

    return 0;
}

28. Menu-Driven Calculator

#include <stdio.h>

int main()
{
    int choice;
    float a, b;

    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Division\n");

    printf("Enter choice: ");
    scanf("%d", &choice);

    printf("Enter two numbers: ");
    scanf("%f %f", &a, &b);

    switch(choice)
    {
        case 1:
            printf("Result = %.2f", a + b);
            break;

        case 2:
            printf("Result = %.2f", a - b);
            break;

        case 3:
            printf("Result = %.2f", a * b);
            break;

        case 4:
            if(b == 0)
                printf("Cannot divide by zero");
            else
                printf("Result = %.2f", a / b);
            break;

        default:
            printf("Invalid choice");
    }

    return 0;
}

29. Student Marks and Grade Program

#include <stdio.h>

int main()
{
    float marks;

    printf("Enter marks: ");
    scanf("%f", &marks);

    if(marks >= 90)
    {
        printf("Grade A");
    }
    else if(marks >= 75)
    {
        printf("Grade B");
    }
    else if(marks >= 60)
    {
        printf("Grade C");
    }
    else if(marks >= 40)
    {
        printf("Grade D");
    }
    else
    {
        printf("Grade F");
    }

    return 0;
}

This example combines input, conditions, comparison operators, and output.

30. Practice Challenge

Create a simple Student Management Program using the concepts learned so far.

Your program should:

  1. Store student ID, name, and marks.
  2. Accept student information from the user.
  3. Calculate the total and average marks.
  4. Determine the grade.
  5. Display the student's details.
  6. Use a structure to store the student.
  7. Use a function to calculate the grade.
  8. Validate marks before processing them.
  9. Save the student's result into a file.
  10. Handle file-opening errors.

This project combines structures, functions, conditions, input/output, file handling, and error handling.

📌 Key Points

  • Practical programs help you apply C concepts to real problems.
  • if and switch are useful for decision-making.
  • Loops are useful for repeated calculations.
  • Arrays store collections of related values.
  • Strings can be processed using functions from string.h.
  • Functions make programs modular and reusable.
  • Pointers can be used to modify values through their addresses.
  • Structures can store related information of different types.
  • File handling allows programs to store and retrieve data.
  • Input validation helps prevent invalid data from being processed.
  • Practice is important for improving C programming skills.

🧠 Quick Quiz

Question: Which operator is commonly used to find the remainder when checking whether a number is even or odd?