Sample Beginner Programs

Chapter 10: Sample Beginner Programs

This chapter consolidates everything we've learned so far—data types, control flow, and functions—into practical, real-world examples. Analyzing these programs will build your "computational thinking" in C.

I. Numerical Logic: Fibonacci Sequence

The Fibonacci sequence is a classic problem in both iterative and recursive programming.

#include <stdio.h>

void print_fibonacci(int n) {
    long long t1 = 0, t2 = 1, nextTerm;

    printf("Fibonacci Series (%d terms): ", n);
    for (int i = 1; i <= n; ++i) {
        printf("%lld ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
    }
    printf("\n");
}

int main() {
    int n = 10;
    print_fibonacci(n);
    return 0;
}

II. Mathematical Logic: Prime Number Check

Finding prime numbers is an essential task in cryptography and algorithm design.

#include <stdio.h>
#include <stdbool.h>

bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int num = 29;
    if (is_prime(num))
        printf("%d is a prime number.\n", num);
    else
        printf("%d is not a prime number.\n", num);
    return 0;
}

III. Data Logic: Simple Bubble Sort

Sorting is the process of arranging data in a specific order. Bubble Sort is the simplest sorting algorithm, ideal for learning how nested loops interact with data.

51428Swap if A > B

#include <stdio.h>

void bubble_sort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap arr[j] and arr[j+1]
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int data[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(data) / sizeof(data[0]);
    bubble_sort(data, n);
    
    printf("Sorted array: ");
    for (int i = 0; i < n; i++) printf("%d ", data[i]);
    printf("\n");
    return 0;
}

IV. Interactive Logic: Calculator with switch

Building a basic command-line tool reinforces user input handling and selection logic.

#include <stdio.h>

int main() {
    char op;
    double n1, n2;

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &op);
    printf("Enter two numbers: ");
    scanf("%lf %lf", &n1, &n2);

    switch (op) {
        case '+': printf("%.1lf + %.1lf = %.1lf\n", n1, n2, n1 + n2); break;
        case '-': printf("%.1lf - %.1lf = %.1lf\n", n1, n2, n1 - n2); break;
        case '*': printf("%.1lf * %.1lf = %.1lf\n", n1, n2, n1 * n2); break;
        case '/': 
            if (n2 != 0) printf("%.1lf / %.1lf = %.1lf\n", n1, n2, n1 / n2); 
            else printf("Error! Division by zero.\n");
            break;
        default: printf("Error! Invalid operator.\n");
    }
    return 0;
}

These programs are the foundations for all the complex software systems you will encounter in your career. Practice writing them from memory before moving to more advanced topics.