Basic Programs in C: A Beginner's Guide

Learning basic C programs is the foundation of mastering the C programming language. This guide introduces key concepts with simple programs to help you understand the core syntax and logic in C. These examples include explanations and best practices for writing clean, efficient code.

 

1. Hello, World! Program

The first program every C programmer writes is the classic “Hello, World!” program.

#include <stdio.h>

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

Explanation:

  • #include <stdio.h>: Includes the standard input-output library.
  • printf(): Prints the output to the console.
  • return 0;: Indicates successful program termination.

2. Add Two Numbers

This program demonstrates how to perform basic arithmetic operations.

#include <stdio.h>

int main() {
    int num1, num2, sum;

    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2); // Take input from user

    sum = num1 + num2; // Add the numbers

    printf("Sum: %d\n", sum);
    return 0;
}
  • Key Concept: Input and output using scanf() and printf().

3. Check Even or Odd

Determine if a number is even or odd.

#include <stdio.h>

int main() {
    int num;

    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num % 2 == 0)
        printf("%d is even.\n", num);
    else
        printf("%d is odd.\n", num);

    return 0;
}
  • Key Concept: Use the modulus operator (%) to check divisibility.

4. Find the Largest of Three Numbers

This program demonstrates the use of conditional statements.

#include <stdio.h>

int main() {
    int num1, num2, num3, largest;

    printf("Enter three integers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    if (num1 > num2 && num1 > num3)
        largest = num1;
    else if (num2 > num3)
        largest = num2;
    else
        largest = num3;

    printf("Largest number: %d\n", largest);
    return 0;
}
  • Key Concept: Nested conditional statements.

5. Simple Calculator

Implement a basic calculator using switch statements.

#include <stdio.h>

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

    printf("Enter an operator (+, -, *, /): ");
    scanf(" %c", &operator); // Note the space before %c

    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);

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

    printf("Result: %.2lf\n", result);
    return 0;
}
  • Key Concept: switch statements for menu-driven programs.

6. Factorial of a Number

Find the factorial of a number using a loop.

#include <stdio.h>

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

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (num < 0)
        printf("Factorial of a negative number is undefined.\n");
    else {
        for (i = 1; i <= num; i++)
            factorial *= i; // Multiply each number

        printf("Factorial of %d is %llu\n", num, factorial);
    }

    return 0;
}
  • Key Concept: Use loops for repetitive calculations.

7. Reverse a Number

Reverse a number entered by the user.

#include <stdio.h>

int main() {
    int num, reverse = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &num);

    while (num != 0) {
        remainder = num % 10;
        reverse = reverse * 10 + remainder;
        num /= 10;
    }

    printf("Reversed number: %d\n", reverse);
    return 0;
}
  • Key Concept: Use loops and arithmetic operations to manipulate numbers.

8. Check if a Number is Prime

Determine if a number is a prime number.

#include <stdio.h>

int main() {
    int num, i, isPrime = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (num <= 1)
        isPrime = 0;
    else {
        for (i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                isPrime = 0;
                break;
            }
        }
    }

    if (isPrime)
        printf("%d is a prime number.\n", num);
    else
        printf("%d is not a prime number.\n", num);

    return 0;
}
  • Key Concept: Loops with early termination (break).

9. Generate Fibonacci Series

Print the Fibonacci series up to a given number of terms.

#include <stdio.h>

int main() {
    int n, i;
    unsigned long long t1 = 0, t2 = 1, nextTerm;

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

    printf("Fibonacci Series: %llu, %llu", t1, t2);

    for (i = 3; i <= n; i++) {
        nextTerm = t1 + t2;
        printf(", %llu", nextTerm);
        t1 = t2;
        t2 = nextTerm;
    }

    printf("\n");
    return 0;
}
  • Key Concept: Use loops and variables to generate sequences.

10. Find the Length of a String

Calculate the length of a string entered by the user.

#include <stdio.h>

int main() {
    char str[100];
    int length = 0;

    printf("Enter a string: ");
    scanf("%s", str);

    while (str[length] != '
#include <stdio.h>
int main() {
char str[100];
int length = 0;
printf("Enter a string: ");
scanf("%s", str);
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
return 0;
}
') { length++; } printf("Length of the string: %d\n", length); return 0; }

Key Concept: Work with strings and character arrays.