C Programming Language Projects for Beginners

Are you looking for some beginner-friendly C programming language projects to work on? Look no further! In this article, we’ll go over 15 fun and educational projects that will help you develop your C programming skills.

Introduction

C is a powerful and versatile programming language that has been around for decades. It is widely used in operating systems, game development, and embedded systems, among other applications. C is also an excellent language for beginners to learn because it is relatively simple and has a straightforward syntax. By working on these C programming projects, you’ll be able to learn the language and improve your coding skills.

Project 1: Hello World

No list of programming projects is complete without the classic “Hello World” program. This project involves writing a simple program that displays the text “Hello, World!” on the screen. It’s a great way to get started with C programming and learn the basic syntax.

				
					#include <stdio.h>

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

				
			

This program includes the standard input/output header file stdio.h and defines a main function which returns an integer value. The printf function is used to output the string “Hello, World!” to the console, followed by a newline character \n. Finally, the program returns the value 0 to indicate successful completion.

Project 2: Calculator

A calculator is a great project for beginners because it involves basic math operations like addition, subtraction, multiplication, and division. You can also add more advanced functions like trigonometric functions, logarithms, and exponentials to make the calculator more complex.

				
					#include <stdio.h>

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

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

    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 '/':
            result = num1 / num2;
            break;
        default:
            printf("Invalid operator\n");
            return 1;
    }

    printf("%.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);

    return 0;
}

				
			

This program prompts the user to enter an operator (+, -, *, or /) and two numbers. It then uses a switch statement to perform the appropriate calculation based on the operator entered by the user. The result is stored in the variable result and printed to the console using printf. The program returns 0 to indicate successful completion. Note that this is a very basic calculator program and does not handle errors or more complex operations.

Project 3: Guessing Game

The guessing game is a fun project that involves generating a random number and prompting the user to guess what it is. You can add features like hints, difficulty levels, and high scores to make the game more challenging.

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

int main() {
    int number, guess, num_guesses = 0;
    int max_guesses = 10; // default number of guesses allowed
    int level; // difficulty level (1-3)
    int high_score = 0; // initialize high score to 0
    char hint;

    srand(time(0)); // seed random number generator

    printf("Welcome to the guessing game!\n");

    do {
        printf("Enter difficulty level (1-3): ");
        scanf("%d", &level);
        switch (level) {
            case 1:
                max_guesses = 15;
                break;
            case 2:
                max_guesses = 10;
                break;
            case 3:
                max_guesses = 5;
                break;
            default:
                printf("Invalid level\n");
                break;
        }
    } while (level < 1 || level > 3);

    number = rand() % 100 + 1; // generate random number between 1 and 100

    printf("I'm thinking of a number between 1 and 100. Can you guess what it is?\n");

    do {
        printf("Guess a number: ");
        scanf("%d", &guess);
        num_guesses++;

        if (guess == number) {
            printf("Congratulations, you guessed the number in %d guesses!\n", num_guesses);
            if (num_guesses < high_score || high_score == 0) {
                high_score = num_guesses;
                printf("New high score: %d guesses!\n", high_score);
            }
        } else if (guess < number) {
            printf("Too low. ");
            if (num_guesses < max_guesses) {
                printf("Try again!\n");
            } else {
                printf("Sorry, you're out of guesses. The number was %d.\n", number);
            }
        } else {
            printf("Too high. ");
            if (num_guesses < max_guesses) {
                printf("Try again!\n");
            } else {
                printf("Sorry, you're out of guesses. The number was %d.\n", number);
            }
        }

        if (num_guesses == max_guesses) {
            printf("You've reached the maximum number of guesses (%d).\n", max_guesses);
            break;
        }

        printf("Would you like a hint? (y/n): ");
        scanf(" %c", &hint);

        if (hint == 'y' || hint == 'Y') {
            if (number % 2 == 0) {
                printf("Hint: The number is even.\n");
            } else {
                printf("Hint: The number is odd.\n");
            }
        }

    } while (guess != number);

    return 0;
}

				
			

This program generates a random number between 1 and 100 using rand() and srand() functions from stdlib.h and time.h header files respectively. It then prompts the user to guess the number within a certain number of attempts, depending on the selected difficulty level. If the user fails to guess the number within the allowed number of attempts, the game ends and the program reveals the correct number. If the user guesses the number correctly, the program congratulates the user and records the number of attempts as the high score if it is lower than the previous high score.

The program also includes a hint feature that provides the user with a clue about whether the number is odd or even upon request. The program prompts the user for a hint after each guess. The user can enter “y” or “Y” to request a hint, or “n” or “N” to decline.

Overall, this program adds some additional features to the basic guessing game to make it more challenging and interesting for the user. The user can select from three difficulty levels and receive hints to help them guess the number. The program also keeps track of the user’s high score, encouraging them to play again and try to beat their previous record.

Project 4: Tic-Tac-Toe

Tic-Tac-Toe is a classic game that involves two players taking turns placing X’s and O’s on a 3×3 grid. The first player to get three in a row wins. This project involves writing a program that allows two players to play Tic-Tac-Toe against each other.

				
					#include <stdio.h>

// Function to print the game board
void printBoard(char board[]) {
    printf("\n");
    printf(" %c | %c | %c \n", board[0], board[1], board[2]);
    printf("---+---+---\n");
    printf(" %c | %c | %c \n", board[3], board[4], board[5]);
    printf("---+---+---\n");
    printf(" %c | %c | %c \n", board[6], board[7], board[8]);
}

// Function to check if the game is over
int gameOver(char board[]) {
    int i;
    // Check for horizontal wins
    for (i = 0; i < 9; i += 3) {
        if (board[i] == board[i+1] && board[i+1] == board[i+2]) {
            return 1;
        }
    }
    // Check for vertical wins
    for (i = 0; i < 3; i++) {
        if (board[i] == board[i+3] && board[i+3] == board[i+6]) {
            return 1;
        }
    }
    // Check for diagonal wins
    if ((board[0] == board[4] && board[4] == board[8]) ||
        (board[2] == board[4] && board[4] == board[6])) {
        return 1;
    }
    // Check for a tie
    for (i = 0; i < 9; i++) {
        if (board[i] == ' ') {
            return 0;
        }
    }
    return 2;
}

// Function to get the user's move
int getMove(char board[], char player) {
    int move;
    printf("\n%s's turn. Enter move (1-9): ", player);
    scanf("%d", &move);
    // Convert move from 1-9 to 0-8 index
    move--;
    if (move < 0 || move > 8 || board[move] != ' ') {
        printf("Invalid move. Try again.\n");
        return getMove(board, player);
    }
    board[move] = player;
    return move;
}

int main() {
    char board[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    char players[2] = {'X', 'O'};
    int currentPlayer = 0;
    int move;
    printf("Welcome to Tic Tac Toe!\n");
    while (gameOver(board) == 0) {
        printBoard(board);
        move = getMove(board, players[currentPlayer]);
        currentPlayer = (currentPlayer + 1) % 2;
    }
    printBoard(board);
    if (gameOver(board) == 1) {
        printf("%s wins!\n", players[(currentPlayer + 1) % 2]);
    } else {
        printf("It's a tie!\n");
    }
    return 0;
}

				
			

This program uses a 1-dimensional array to represent the 3×3 game board, and uses functions to print the board, get the user’s move, and check if the game is over. The main function uses a while loop to continue the game until it is over, and prints the winner or a tie at the end.

Project 5: Hangman

Hangman is a popular game that involves guessing a word by guessing letters one at a time. The player has a limited number of guesses before they lose. This project involves writing a program that generates a random word and allows the user to guess letters until they either guess the word or run out of guesses.

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

#define MAX_WORDS 10
#define MAX_WORD_LENGTH 20
#define MAX_GUESSES 6

char words[MAX_WORDS][MAX_WORD_LENGTH] = {"apple", "banana", "cherry", "orange", "peach", "pear", "grape", "kiwi", "mango", "pineapple"};
char guessed_word[MAX_WORD_LENGTH];
int wrong_guesses = 0;

void init_game() {
    srand(time(NULL));
    int word_index = rand() % MAX_WORDS;
    strcpy(guessed_word, words[word_index]);
}

void print_word() {
    int i;
    for (i = 0; i < strlen(guessed_word); i++) {
        printf("%c ", guessed_word[i]);
    }
    printf("\n");
}

void print_hangman() {
    switch(wrong_guesses) {
        case 0:
            printf("\n\n\n\n\n\n");
            break;
        case 1:
            printf("\n\n\n\n\n______\n");
            break;
        case 2:
            printf(" |\n |\n |\n |\n_|\n");
            break;
        case 3:
            printf(" ______\n |\n |\n |\n_|\n");
            break;
        case 4:
            printf(" ______\n |     |\n |\n |\n_|\n");
            break;
        case 5:
            printf(" ______\n |     |\n |     O\n |\n_|\n");
            break;
        case 6:
            printf(" ______\n |     |\n |     O\n |    /|\\\n |    / \\\n_|\n");
            break;
    }
}

int check_guess(char c) {
    int i;
    int found = 0;
    for (i = 0; i < strlen(guessed_word); i++) {
        if (c == guessed_word[i]) {
            found = 1;
            break;
        }
    }
    if (!found) {
        wrong_guesses++;
    }
    return found;
}

int check_win() {
    int i;
    for (i = 0; i < strlen(guessed_word); i++) {
        if (guessed_word[i] == '_') {
            return 0;
        }
    }
    return 1;
}

int main() {
    init_game();
    printf("Welcome to Hangman!\n");
    printf("You have %d guesses to guess the word.\n", MAX_GUESSES);
    while (wrong_guesses < MAX_GUESSES) {
        printf("\n\n");
        print_hangman();
        print_word();
        char guess;
        printf("Guess a letter: ");
        scanf(" %c", &guess);
        if (check_guess(guess)) {
            printf("Correct!\n");
        } else {
            printf("Incorrect.\n");
        }
        if (check_win()) {
            printf("\n\n");
            print_word();
            printf("Congratulations! You guessed the word.\n");
            return 0;
        }
    }
    printf("\n\n");
    print_hangman();
    printf("Sorry, you ran out of guesses. The word was %s.\n", guessed_word);
    return 0;
}

				
			

The program uses several functions to perform different tasks:

The init_game() function initializes the game by randomly selecting a word from the word list and copying it to the guessed_word array.

The print_word() function prints the current state of the guessed word, with letters that have not yet been guessed shown as underscores.

The print_hangman() function prints the current state of the hangman, based on the number of wrong guesses the player has made so far.

The check_guess() function checks if a guessed letter is present in the guessed_word array. If it is, it returns 1; otherwise, it increments the wrong_guesses counter and returns 0.

The check_win() function checks if all letters in the guessed_word array have been guessed correctly.

The main() function starts the game by calling init_game() and printing the welcome message. It then enters a loop where it repeatedly asks the player to guess a letter and checks if the guess is correct. If the player runs out of guesses before guessing the word, the game is over and the program prints the hangman and reveals the word.

Overall, this program provides a basic implementation of the Hangman game in C. However, there are many possible ways to improve and expand the game, such as allowing the player to choose a category or difficulty level, adding a graphical user interface, or using a more extensive word list.

Project 6: Fahrenheit to Celsius Converter

This project involves writing a program that converts temperatures from Fahrenheit to Celsius. It’s a simple project that involves basic arithmetic and can be a good way to practice using variables and basic data types.

				
					#include <stdio.h>

int main() {
    float fahrenheit, celsius;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    celsius = (fahrenheit - 32) * 5 / 9;
    printf("%.2f Fahrenheit is equal to %.2f Celsius.", fahrenheit, celsius);
    return 0;
}

				
			

This program starts by declaring two variables: fahrenheit and celsius. The printf() function is then used to ask the user to enter a temperature in Fahrenheit. The scanf() function is used to read the user’s input and store it in the fahrenheit variable.

The program then calculates the equivalent temperature in Celsius using the formula (F - 32) * 5/9, where F is the temperature in Fahrenheit. The result is stored in the celsius variable.

Finally, the program uses another printf() function to display the original temperature in Fahrenheit and the converted temperature in Celsius, with two decimal places of precision.

This program provides a simple example of how to convert temperatures from Fahrenheit to Celsius in C. However, it could be extended or modified to include additional features, such as input validation or conversion in the opposite direction.

Project 7: Prime Number Checker

A prime number is a number that is only divisible by 1 and itself. This project involves writing a program that checks whether a number is prime or not. It’s a great way to practice using loops and conditional statements.

				
					#include <stdio.h>

int main() {
    int num, i, flag = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    for (i = 2; i <= num / 2; ++i) {
        if (num % i == 0) {
            flag = 1;
            break;
        }
    }
    if (num == 1) {
        printf("1 is neither prime nor composite.");
    }
    else {
        if (flag == 0)
            printf("%d is a prime number.", num);
        else
            printf("%d is not a prime number.", num);
    }
    return 0;
}

				
			

This program starts by declaring three variables: num, i, and flag. The printf() function is then used to ask the user to enter a positive integer, which is stored in the num variable using the scanf() function.

The program then uses a for loop to check if num is divisible by any integer from 2 to num/2. If it is, the flag variable is set to 1 and the loop is exited using the break statement.

After the loop, the program checks if num is equal to 1. If it is, it prints a message stating that 1 is neither prime nor composite. Otherwise, it checks the value of flag. If flag is 0, it prints a message stating that num is a prime number. Otherwise, it prints a message stating that num is not a prime number.

Overall, this program provides a basic implementation of a prime number checker in C. However, it does not include any optimizations or advanced techniques for checking if a number is prime, such as the Sieve of Eratosthenes or the Miller-Rabin primality test.

Project 8: File Encryption

File encryption involves scrambling the contents of a file so that it cannot be read by anyone who does not have the key to unscramble it. This project involves writing a program that encrypts and decrypts files using a simple encryption algorithm.

				
					#include <stdio.h>

#define KEY 'R' // encryption/decryption key

void encryptFile(char* fileName);
void decryptFile(char* fileName);

int main() {
    char fileName[100];
    int choice;

    printf("Enter file name: ");
    scanf("%s", fileName);

    printf("Enter your choice:\n");
    printf("1. Encrypt file\n");
    printf("2. Decrypt file\n");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            encryptFile(fileName);
            break;
        case 2:
            decryptFile(fileName);
            break;
        default:
            printf("Invalid choice.\n");
    }

    return 0;
}

void encryptFile(char* fileName) {
    FILE* fp1, *fp2;
    char ch;

    fp1 = fopen(fileName, "r");
    fp2 = fopen("encrypted.txt", "w");

    if (fp1 == NULL || fp2 == NULL) {
        printf("Error opening file.\n");
        return;
    }

    while ((ch = fgetc(fp1)) != EOF) {
        ch = ch ^ KEY;
        fputc(ch, fp2);
    }

    fclose(fp1);
    fclose(fp2);

    printf("File encrypted successfully.\n");
}

void decryptFile(char* fileName) {
    FILE* fp1, *fp2;
    char ch;

    fp1 = fopen(fileName, "r");
    fp2 = fopen("decrypted.txt", "w");

    if (fp1 == NULL || fp2 == NULL) {
        printf("Error opening file.\n");
        return;
    }

    while ((ch = fgetc(fp1)) != EOF) {
        ch = ch ^ KEY;
        fputc(ch, fp2);
    }

    fclose(fp1);
    fclose(fp2);

    printf("File decrypted successfully.\n");
}

				
			

This program defines two functions: encryptFile() and decryptFile(). Both functions take a file name as input, open the file for reading or writing, and use a simple XOR operation with a predefined key (KEY) to encrypt or decrypt the file.

In the main function, the program asks the user to enter a file name and a choice between encrypting or decrypting the file. It then calls the appropriate function based on the user’s choice.

The encryptFile() function opens the input file for reading and creates a new file called “encrypted.txt” for writing the encrypted data. It reads each character from the input file, XORs it with the encryption key, and writes the result to the output file. After all characters have been processed, both files are closed and a success message is printed.

The decryptFile() function works in a similar way, but instead of creating a new file, it creates a file called “decrypted.txt” to write the decrypted data.

Note that this is a very basic encryption algorithm and should not be used for real-world security purposes. There are many other encryption algorithms and libraries available in C that provide stronger security guarantees.

Project 9: Morse Code Translator

Morse code is a system of dots and dashes that can be used to transmit messages over long distances. This project involves writing a program that translates text into Morse code and vice versa.

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

char * morseCode(char letter);

int main() {
    char text[1000];
    printf("Enter text to convert to Morse code: ");
    fgets(text, sizeof(text), stdin);

    printf("Morse code: ");
    for (int i = 0; i < strlen(text); i++) {
        if (text[i] == ' ') {
            printf(" / ");
        } else if (isalnum(text[i])) {
            printf("%s ", morseCode(toupper(text[i])));
        }
    }

    return 0;
}

char * morseCode(char letter) {
    static char * code[] = {
        ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
        ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
        "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
    };

    if (isalpha(letter)) {
        return code[letter - 'A'];
    } else if (isdigit(letter)) {
        return code[letter - '0' + 26];
    } else {
        return "";
    }
}

				
			

This program first declares a function morseCode() that takes a character as input and returns the corresponding Morse code string. Morse code is stored in an array of strings, with letters represented by dots and dashes.

In the main function, the program asks the user to enter text to convert to Morse code using fgets() function to read input string. It then iterates through each character in the input string and checks if it is a space, a letter, or a digit. Spaces are replaced with the Morse code for a forward slash, while letters and digits are passed to the morseCode() function to retrieve the corresponding Morse code string. If the input character is not a space, a letter, or a digit, nothing is printed.

The morseCode() function uses an array of strings to represent each letter and digit in Morse code. It takes a single character as input, checks if it is an alphabetic character or a digit, and returns the corresponding Morse code string. If the input character is not a letter or a digit, an empty string is returned.

Overall, this program provides a simple implementation of a Morse code translator in C. However, it does not include any error handling or input validation, and its support for non-English characters and punctuation marks is limited.

Project 10: ASCII Art Generator

ASCII art is a type of art that uses characters from the ASCII character set to create images. This project involves writing a program that converts images into ASCII art.

				
					#include <stdio.h>

int main() {
    printf("            /\\_/\\\n");
    printf("           ( o o )\n");
    printf("          ==_~_~_~_==\n");
    printf("           /     \\\n");
    printf("          /       \\\n");
    printf("         /         \\\n");
    printf("        /           \\\n");
    printf("       /             \\\n");
    printf("      /_______________\\\n");

    return 0;
}

				
			

This program uses the printf() function to print out an ASCII art image of a cat. The image is created by printing out a series of backslashes, forward slashes, underscores, and other characters in a specific arrangement to form the shape of the cat.

The backslashes and forward slashes are used to create diagonal lines for the cat’s ears, head, and body, while underscores are used to create the cat’s legs and tail. Tildes and equal signs are used to create the cat’s whiskers and eyes, respectively.

Overall, this program provides a simple example of how ASCII art can be generated in C. However, it is limited to a specific image and does not include any input or customization options.

Project 11: Encryption Decryption Tool

This project involves writing a program that can encrypt and decrypt messages using a simple encryption algorithm. It’s a great way to practice using functions and pointers.

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

char * encrypt(char * message, char * key);
char * decrypt(char * message, char * key);

int main() {
    char message[1000];
    char key[26];
    int choice;

    printf("Enter message to encrypt/decrypt: ");
    fgets(message, sizeof(message), stdin);
    message[strcspn(message, "\n")] = 0; // Remove newline character from input

    printf("Enter encryption key (must be 26 unique letters): ");
    fgets(key, sizeof(key), stdin);
    key[strcspn(key, "\n")] = 0; // Remove newline character from input

    printf("Enter 1 to encrypt, 2 to decrypt: ");
    scanf("%d", &choice);

    if (choice == 1) {
        printf("Encrypted message: %s\n", encrypt(message, key));
    } else if (choice == 2) {
        printf("Decrypted message: %s\n", decrypt(message, key));
    } else {
        printf("Invalid choice.\n");
    }

    return 0;
}

char * encrypt(char * message, char * key) {
    static char result[1000];
    int len = strlen(message);

    for (int i = 0; i < len; i++) {
        if (message[i] == ' ') {
            result[i] = ' ';
        } else {
            int index = message[i] - 'A';
            result[i] = key[index];
        }
    }

    result[len] = '\0';
    return result;
}

char * decrypt(char * message, char * key) {
    static char result[1000];
    int len = strlen(message);

    for (int i = 0; i < len; i++) {
        if (message[i] == ' ') {
            result[i] = ' ';
        } else {
            int index = strchr(key, message[i]) - key;
            result[i] = 'A' + index;
        }
    }

    result[len] = '\0';
    return result;
}

				
			

This program first prompts the user to enter a message to encrypt/decrypt and an encryption key, which must be a string of 26 unique letters. It then prompts the user to choose between encrypting and decrypting the message.

If the user chooses to encrypt the message, the program calls the encrypt() function, which takes the message and encryption key as input and returns the encrypted message. The function substitutes each letter in the message with the corresponding letter in the encryption key, preserving spaces.

If the user chooses to decrypt the message, the program calls the decrypt() function, which takes the encrypted message and encryption key as input and returns the decrypted message. The function performs the inverse substitution of the encrypt() function to recover the original message.

Both the encrypt() and decrypt() functions use a static buffer to store the result, which is returned as a string. The buffer is reused for subsequent function calls, so the program is not thread-safe.

Overall, this program provides a basic implementation of an encryption/decryption tool in C using a substitution cipher. However, the substitution cipher used is not very secure and can be easily cracked using frequency analysis. Additionally, the program does not include any error handling or input validation.

Project 12: Text-Based Adventure Game

A text-based adventure game is a game where the player navigates a story by making choices at different points in the game. This project involves writing a program that allows the user to navigate a story by making choices. The program should present the user with different options at each point in the story and change the story based on the user’s choices.

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

int main() {
    char answer[50];

    printf("Welcome to the Adventure Game!\n\n");
    printf("You find yourself in a dark room. There are two doors, one to your left and one to your right. Which one do you choose? (left/right)\n");

    scanf("%s", answer);

    if(strcmp(answer, "left") == 0) {
        printf("\nYou enter a room filled with treasure! Congratulations, you win!\n");
    }
    else if(strcmp(answer, "right") == 0) {
        printf("\nYou enter a room with a hungry lion. Game over!\n");
    }
    else {
        printf("\nThat's not a valid answer. Game over!\n");
    }

    return 0;
}

				
			

In this game, the player starts in a dark room and is given a choice between two doors. If they choose the left door, they win the game by finding treasure. If they choose the right door, they encounter a hungry lion and lose the game. If they enter an invalid response, the game ends as well.

Of course, this is just a simple example and you can add more complexity to the game by adding more rooms, items, enemies, puzzles, and other interactive elements.

Project 13: Password Manager

A password manager is a program that stores and organizes passwords for various accounts. This project involves writing a program that allows the user to store and retrieve passwords for different accounts.

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

#define MAX_ACCOUNTS 10
#define MAX_USERNAME_LENGTH 20
#define MAX_PASSWORD_LENGTH 20

struct Account {
    char username[MAX_USERNAME_LENGTH];
    char password[MAX_PASSWORD_LENGTH];
};

struct Account accounts[MAX_ACCOUNTS];
int num_accounts = 0;

void add_account() {
    if(num_accounts >= MAX_ACCOUNTS) {
        printf("Error: Maximum number of accounts reached.\n");
        return;
    }

    struct Account account;

    printf("Enter a new username: ");
    scanf("%s", account.username);

    printf("Enter a new password: ");
    scanf("%s", account.password);

    accounts[num_accounts++] = account;

    printf("Account added successfully.\n");
}

void list_accounts() {
    if(num_accounts == 0) {
        printf("No accounts found.\n");
        return;
    }

    printf("Accounts:\n");

    for(int i = 0; i < num_accounts; i++) {
        printf("%d. %s\n", i+1, accounts[i].username);
    }
}

void view_password() {
    if(num_accounts == 0) {
        printf("No accounts found.\n");
        return;
    }

    printf("Enter the account number: ");

    int account_number;
    scanf("%d", &account_number);

    if(account_number < 1 || account_number > num_accounts) {
        printf("Invalid account number.\n");
        return;
    }

    printf("Password for %s: %s\n", accounts[account_number-1].username, accounts[account_number-1].password);
}

int main() {
    int choice;

    while(1) {
        printf("\n1. Add an account\n");
        printf("2. List accounts\n");
        printf("3. View password\n");
        printf("4. Quit\n");

        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                add_account();
                break;
            case 2:
                list_accounts();
                break;
            case 3:
                view_password();
                break;
            case 4:
                printf("Goodbye!\n");
                return 0;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }
}

				
			

In this password manager, the user can add new accounts by entering a username and password, list all existing accounts, and view the password for a specific account by selecting its account number. The maximum number of accounts is set to 10 in this example, but you can change this by modifying the MAX_ACCOUNTS constant. The maximum length of the username and password fields is set to 20 in this example, but you can change this by modifying the MAX_USERNAME_LENGTH and MAX_PASSWORD_LENGTH constants.

Project 14: Tower of Hanoi

The Tower of Hanoi is a classic puzzle game that involves moving disks between three pegs. The goal is to move all of the disks from the first peg to the third peg without placing a larger disk on top of a smaller disk. This project involves writing a program that solves the Tower of Hanoi puzzle.

				
					#include <stdio.h>

void tower_of_hanoi(int num_disks, char source, char auxiliary, char destination) {
    if(num_disks == 1) {
        printf("Move disk 1 from %c to %c\n", source, destination);
        return;
    }

    tower_of_hanoi(num_disks - 1, source, destination, auxiliary);
    printf("Move disk %d from %c to %c\n", num_disks, source, destination);
    tower_of_hanoi(num_disks - 1, auxiliary, source, destination);
}

int main() {
    int num_disks;

    printf("Enter the number of disks: ");
    scanf("%d", &num_disks);

    tower_of_hanoi(num_disks, 'A', 'B', 'C');

    return 0;
}

				
			

In this program, the user enters the number of disks for the Tower of Hanoi problem. The tower_of_hanoi() function takes three parameters: the number of disks, the source tower, the auxiliary tower, and the destination tower. The function first checks if there is only one disk to move, in which case it simply moves it from the source tower to the destination tower. Otherwise, it recursively calls itself with the number of disks decreased by one and with the source and auxiliary towers swapped. After the recursive call, it prints the movement of the current disk from the source tower to the destination tower. Finally, it makes another recursive call with the number of disks decreased by one and with the auxiliary and destination towers swapped.

When the program is run, it will print the sequence of moves needed to solve the Tower of Hanoi problem with the given number of disks. The solution involves moving all the disks from the source tower to the destination tower using the auxiliary tower as an intermediate step, while never placing a larger disk on top of a smaller one.

Project 15: Sudoku Solver

Sudoku is a popular number puzzle game that involves filling in a 9×9 grid with numbers so that each row, column, and 3×3 box contains all of the digits from 1 to 9. This project involves writing a program that solves Sudoku puzzles.

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

#define N 9 // Sudoku grid size

bool find_empty_location(int grid[N][N], int *row, int *col);

bool is_safe(int grid[N][N], int row, int col, int num);

bool solve_sudoku(int grid[N][N])
{
    int row, col;
    if (!find_empty_location(grid, &row, &col))
       return true; // no empty location left, puzzle solved
    
    for (int num = 1; num <= 9; num++)
    {
        if (is_safe(grid, row, col, num))
        {
            grid[row][col] = num;
            if (solve_sudoku(grid))
                return true;
            grid[row][col] = 0; // backtrack
        }
    }
    return false; // backtracking
}

bool find_empty_location(int grid[N][N], int *row, int *col)
{
    for (*row = 0; *row < N; (*row)++)
        for (*col = 0; *col < N; (*col)++)
            if (grid[*row][*col] == 0)
                return true;
    return false;
}

bool used_in_row(int grid[N][N], int row, int num)
{
    for (int col = 0; col < N; col++)
        if (grid[row][col] == num)
            return true;
    return false;
}

bool used_in_col(int grid[N][N], int col, int num)
{
    for (int row = 0; row < N; row++)
        if (grid[row][col] == num)
            return true;
    return false;
}

bool used_in_box(int grid[N][N], int box_start_row, int box_start_col, int num)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (grid[row+box_start_row][col+box_start_col] == num)
return true;
return false;
}

bool is_safe(int grid[N][N], int row, int col, int num)
{
// Check if number is already used in row, column or box
return !used_in_row(grid, row, num) &&
!used_in_col(grid, col, num) &&
!used_in_box(grid, row - row%3, col - col%3, num) &&
grid[row][col] == 0; // Check if empty
}

void print_grid(int grid[N][N])
{
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
printf("%d ", grid[row][col]);
printf("\n");
}
}

int main()
{
int grid[N][N] = {{0, 3, 0, 0, 8, 0, 4, 0, 0},
{0, 0, 7, 0, 0, 0, 6, 0, 0},
{0, 0, 0, 6, 0, 0, 0, 8, 0},
{0, 7, 0, 0, 0, 5, 0, 9, 0},
{0, 0, 1, 0, 9, 0, 5, 0, 0},
{0, 4, 0, 3, 0, 0, 0, 7, 0},
{0, 6, 0, 0, 0, 9, 0, 0, 0},
{0, 0, 8, 0, 0, 0, 9, 0, 0},
{0, 0, 3, 0, 4, 0, 0, 5, 0}};
if (solve_sudoku(grid) == true)
print_grid(grid);
else
printf("No solution exists\n");
return 0;
}

				
			

This program solves a Sudoku puzzle using a recursive backtracking algorithm. The main function initializes a 2D array `grid` to represent the Sudoku puzzle, and then calls the `solve_sudoku` function to solve the puzzle. If a solution exists, the function returns `true` and the `print_grid` function is called to display the solution. Otherwise, the function returns `false` and a message indicating that no solution exists is displayed.

The `solve_sudoku` function uses a recursive backtracking algorithm to solve the puzzle. It first calls the `find_empty_location` function to find an empty cell in the grid. If no empty cell exists, the function returns `true` to indicate that the puzzle has been solved. Otherwise, the function tries each number from 1 to 9 in the empty cell, and checks if it is safe to place the number there using the `is_safe` function. If it is safe, the number is placed in the cell, and the function calls itself recursively to solve the remaining puzzle. If a solution is found, the function

returns true. If a solution is not found, the function backtracks by resetting the value of the cell to 0 and trying the next number. If all numbers have been tried and no solution has been found, the function returns false to indicate that backtracking is necessary.

The find_empty_location function scans the grid for an empty cell and returns its coordinates using pointers.

The is_safe function checks if it is safe to place a number in a given cell. It does this by checking if the number is already present in the same row, column, or 3×3 box as the cell, and whether the cell is empty or not.

The used_in_row, used_in_col, and used_in_box functions check if a number is already present in a given row, column, or 3×3 box, respectively.

The print_grid function simply prints out the solution grid in a readable format.

Overall, this program provides a simple and effective solution to solve Sudoku puzzles using C.

Conclusion

Working on these C programming language projects is a great way to develop your skills and have fun at the same time. Whether you’re a beginner or an experienced programmer, these projects will help you improve your coding skills and become more proficient in C programming.

FAQs

C programming language is used for a wide range of applications, including operating systems, game development, and embedded systems.

C programming language has a relatively simple syntax, but it can be challenging to learn for beginners.

Some good resources for learning C programming language include online tutorials, books, and video courses.

The amount of time it takes to learn C programming language depends on your experience and dedication. With consistent practice, you can become proficient in C programming in a few months.

Some advanced C programming language projects include building an operating system, creating a game engine, and developing an embedded system.

You can also visit my Hindi YouTube Channel.

Leave a Comment