Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C Programming Examples: Functions, Recursion, and Algorithms

Tech 1

This article presents several C programming examples that demonstrate the use of functions, recursion, and various algorithms.

1. Power Calculation Using Recursion

This program calculates the power of a number using a recursive function with optimization for even exponents.

#include <stdio.h>
#include <stdlib.h>

int compute_power(int base, int exponent);

int main() {
    int base, exponent;
    int result;
    
    while (printf("Enter base and exponent: "), scanf("%d%d", &base, &exponent) != EOF) {
        result = compute_power(base, exponent);
        printf("Exponent = %d, Result = %d\n\n", exponent, result);
    }
    
    system("pause");
    return 0;
}

int compute_power(int base, int exponent) {
    int temp;
    
    if (exponent == 0)
        return 1;
    else if (exponent % 2)
        return base * compute_power(base, exponent - 1);
    else {
        temp = compute_power(base, exponent / 2);
        return temp * temp;
    }
}

2. Score to Grade Conversion

This program converts numerical scores to letter grades using a switch statement.

#include <stdio.h>
#include <stdlib.h>

char convert_score_to_grade(int score);

int main() {
    int score;
    char grade;
    
    while (scanf("%d", &score) != EOF) {
        grade = convert_score_to_grade(score);
        printf("Score: %d, Grade: %c\n\n", score, grade);
    }
    
    system("pause");
    return 0;
}

char convert_score_to_grade(int score) {
    char grade;
    
    switch (score / 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;
}

3. Sum of Digits Calculation

This program calculates the sum of digits of a given integer.

#include <stdio.h>
#include <stdlib.h>

int calculate_digit_sum(int number);

int main() {
    int number;
    int result;
    
    while (printf("Enter number: "), scanf("%d", &number) != EOF) {
        result = calculate_digit_sum(number);
        printf("Number = %d, Sum of digits = %d\n\n", number, result);
    }
    
    system("pause");
    return 0;
}

int calculate_digit_sum(int number) {
    int sum = 0;
    
    while (number != 0) {
        sum += number % 10;
        number /= 10;
    }
    
    return sum;
}

4. Prime Number and Twin Prime Identification

This program identifies prime numbers and counts twin primes (pairs of primes with difference 2) up to 100.

#include <stdio.h>
#include <stdlib.h>

int check_prime(int number);

int main() {
    int number;
    int count = 0;
    int prime1, prime2;
    
    for (number = 1; number <= 98; number++) {
        prime1 = check_prime(number);
        prime2 = check_prime(number + 2);
        
        if (prime1 == 1 && prime2 == 1) {
            printf("%d %d\n", number, number + 2);
            count += 1;
        }
    }
    
    printf("Number of twin primes under 100: %d\n", count);
    system("pause");
    return 0;
}

int check_prime(int number) {
    int divisor;
    
    if (number == 1)
        return 0;
    
    for (divisor = 2; divisor <= number / 2; divisor++) {
        if (number % divisor == 0)
            return 0;
    }
    
    return 1;
}

5. Tower of Hanoi Problem

This program solves the Tower of Hanoi problem using recursion and counts the number of moves.

#include <stdio.h>
#include <stdlib.h>

void solve_hanoi(unsigned int disks, char source, char auxiliary, char destination);
int move_disk(unsigned int disk, char source, char destination);
int move_count;

int main() {
    unsigned int disks;
    
    while (scanf("%u", &disks) != EOF) {
        solve_hanoi(disks, 'A', 'B', 'C');
        printf("Total moves: %d.\n", move_count);
        move_count = 0;
    }
    
    system("pause");
    return 0;
}

void solve_hanoi(unsigned int disks, char source, char auxiliary, char destination) {
    if (disks == 1) {
        move_disk(disks, source, destination);
    } else {
        solve_hanoi(disks - 1, source, destination, auxiliary);
        move_disk(disks, source, destination);
        solve_hanoi(disks - 1, auxiliary, source, destination);
    }
    move_count += 1;
}

int move_disk(unsigned int disk, char source, char destination) {
    printf("%u: %c -> %c\n", disk, source, destination);
}

6. Combination Calculation (Iterative and Recursive)

This program calculates combinations using both iterative and recursive approaches.

Iterative Version

#include <stdio.h>
#include <stdlib.h>

int calculate_combination(int n, int m);

int main() {
    int n, m;
    int result;
    
    while (scanf("%d%d", &n, &m) != EOF) {
        result = calculate_combination(n, m);
        printf("n = %d, m = %d, Combination = %d\n\n", n, m, result);
    }
    
    system("pause");
    return 0;
}

int calculate_combination(int n, int m) {
    int numerator = 1, denominator = 1, result;
    int i, j;
    
    if (n < m)
        return 0;
    else {
        for (i = n; i >= (n - m + 1); i--)
            numerator *= i;
        for (j = 1; j <= m; j++)
            denominator *= j;
        result = numerator / denominator;
        return result;
    }
}

Rceursive Version

#include <stdio.h>
#include <stdlib.h>

int calculate_combination(int n, int m);

int main() {
    int n, m;
    int result;
    
    while (scanf("%d%d", &n, &m) != EOF) {
        result = calculate_combination(n, m);
        printf("n = %d, m = %d, Combination = %d\n\n", n, m, result);
    }
    
    system("pause");
    return 0;
}

int calculate_combination(int n, int m) {
    int result;
    
    if (n < m)
        return 0;
    else if (m == 0)
        return 1;
    else if (m == 1)
        result = n;
    else
        result = calculate_combination(n - 1, m) + calculate_combination(n - 1, m - 1);
    
    return result;
}

7. Greatest Common Divisor of Three Numbers

This program calculates the greatest common divisor (GCD) of three numbers.

#include <stdio.h>
#include <stdlib.h>

int find_gcd(int a, int b, int c);

int main() {
    int a, b, c;
    int result;
    
    while (scanf("%d%d%d", &a, &b, &c) != EOF) {
        result = find_gcd(a, b, c);
        printf("GCD: %d\n\n", result);
    }
    
    system("pause");
    return 0;
}

int find_gcd(int a, int b, int c) {
    int minimum = a;
    
    if (b < minimum) {
        minimum = b;
    }
    
    if (c < minimum) {
        minimum = c;
    }
    
    for (int i = minimum; i >= 1; i--) {
        if (a % i == 0 && b % i == 0 && c % i == 0) {
            return i;
        }
    }
    
    return 1;
}

These examples cover various programming concepts including function implementation, recursion, mathematical calculasions, and algorithm design in C programming.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

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...

Leave a Comment

Anonymous

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