Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C Programming Examples: Functions and Recursion

Tech May 18 34

Score to Grade Conversion

This program converts numerical scores to letter grades using a switch-case structure.

#include <stdio.h>

char calculate_grade(int value);

int main() {
    int input;
    char result;

    while (scanf("%d", &input) != EOF) {
        result = calculate_grade(input);
        printf("Score: %d, Grade: %c\n\n", input, result);
    }
    return 0;
}

char calculate_grade(int value) {
    char grade;
    switch (value / 10) {
        case 10:
        case 9: grade = 'A'; break;
        case 8: grade = 'B'; break;
        case 7: grade = 'C'; break;
        case 6: grade = 'D'; break;
        default: grade = 'E';
    }
    return grade;
}

Digit Sum Calculator

Calculates the sum of digits in an integer using iterative modulus operations.

#include <stdio.h>

int digit_sum(int num);

int main() {
    int value, total;
    while (printf("Enter value: "), scanf("%d", &value) != EOF) {
        total = digit_sum(value);
        printf("Value: %d, Sum: %d\n\n", value, total);
    }
    return 0;
}

int digit_sum(int num) {
    int total = 0;
    while (num != 0) {
        total += num % 10;
        num /= 10;
    }
    return total;
}

Twin Prime Identification

Identifies twin prime pairs within 100 using a primality test function.

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

int check_prime(int num);

int main() {
    int count = 0;
    printf("Twin primes under 100:\n");
    for (int i = 2; i <= 98; ++i) {
        if (check_prime(i) && check_prime(i + 2)) {
            printf("%d %d\n", i, i + 2);
            count++;
        }
    }
    printf("Total twin prime pairs: %d\n", count);
    return 0;
}

int check_prime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) return 0;
    }
    return 1;
}

Tower of Hanoi Solver

Recursive solution for Tower of Hanoi with move counting.

#include <stdio.h>

unsigned total_moves = 0;
void solve_hanoi(unsigned disks, char src, char aux, char dest);
void log_move(unsigned disk, char from, char to);

int main() {
    unsigned disk_count;
    while (scanf("%u", &disk_count) != EOF) {
        total_moves = 0;
        solve_hanoi(disk_count, 'A', 'B', 'C');
        printf("Total moves: %u\n", total_moves);
        getchar();
    }
    return 0;
}

void solve_hanoi(unsigned disks, char src, char aux, char dest) {
    if (disks == 1) {
        log_move(disks, src, dest);
    } else {
        solve_hanoi(disks - 1, src, dest, aux);
        log_move(disks, src, dest);
        solve_hanoi(disks - 1, aux, src, dest);
    }
}

void log_move(unsigned disk, char from, char to) {
    printf(" %u: %c -> %c\n", disk, from, to);
    total_moves++;
}

Combinatorial Calculator

Computes combinations using recursvie mathematical relationships.

#include <stdio.h>

int combination(int n, int m);

int main() {
    int n_val, m_val, result;
    while (scanf("%d%d", &n_val, &m_val) != EOF) {
        result = combination(n_val, m_val);
        printf("n=%d, m=%d, result=%d\n\n", n_val, m_val, result);
    }
    return 0;
}

int combination(int n, int m) {
    if (m == 0 || m == n) return 1;
    if (n < m) return 0;
    return combination(n - 1, m) + combination(n - 1, m - 1);
}

Recursive Character Pattern

Generates an indanted character pattern using recursion.

#include <stdio.h>

int indent_level = 0;
void print_figure(int size);

int main() {
    int size;
    printf("Enter size: ");
    scanf("%d", &size);
    print_figure(size);
    return 0;
}

void print_figure(int size) {
    if (size <= 0) return;
    
    for (int i = 0; i < indent_level; i++) printf("\t");
    for (int j = 0; j < 2 * size - 1; j++) printf(" O      ");
    printf("\n");
    
    for (int i = 0; i < indent_level; i++) printf("\t");
    for (int i = 0; i < 2 * size - 1; i++) printf("<H>\t");
    printf("\n");
    
    for (int i = 0; i < indent_level; i++) printf("\t");
    for (int i = 0; i < 2 * size - 1; i++) printf("I I\t");
    printf("\n");
    
    indent_level++;
    print_figure(size - 1);
}

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.