Monday, September 8, 2025

Number System Conversion || C Code Projects for School students

 Reference: CCP_L01_C08_Number System Conversion

Introduction

In the digital world, number systems play a crucial role in computer operations. Understanding Binary (2), Decimal (10), Octal (8), and Hexadecimal (16) is essential for programming, networking, and embedded systems.

This blog post explores a C programming project that allows conversion between different number systems, making it an excellent academic project for school students (ages 8-16).


Project Overview

This project is developed using C language with a simple menu-driven approach, following best coding practices and file handling techniques. The Code::Blocks IDE with MinGW C Compiler is used for development.

Features of the Project

     Convert numbers between Binary, Decimal, Octal, and Hexadecimal.

     Menu-driven system for easy user interaction.

     Stores past conversions in conversion_log.txt.

     Uses file handling for data storage.

     Implements basic error handling to validate inputs.


How the System Works?

🔹 The user selects an option from the menu:
 ✔ Convert a number (Input base & Target base).
 ✔ View past conversions.
 ✔ Exit the program.

🔹 If conversion is chosen, the user enters a number in a selected base, and the program converts it to the desired base.
 ðŸ”¹ The result is displayed on-screen and stored in
conversion_log.txt.


C Code for Number System Conversion

This C code provides a structured and modular implementation:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <string.h>

 

#define MAX_DIGITS 32

 

void menu();

void getInput();

void decimalToOther(int, int);

int otherToDecimal(char[], int);

void storeInFile(int, char[], int, int);

void viewPastConversions();

 

int main() {

    menu();

    return 0;

}

 

void menu() {

    int choice;

    while (1) {

        printf("\\nNumber System Conversion in C\\n");

        printf("1. Convert a Number\\n");

        printf("2. View Past Conversions\\n");

        printf("3. Exit\\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        if (choice == 1)

            getInput();

        else if (choice == 2)

            viewPastConversions();

        else if (choice == 3) {

            printf("Exiting program...\\n");

            exit(0);

        } else

            printf("Invalid choice! Try again.\\n");

    }

}

 

void getInput() {

    int fromBase, toBase;

    char number[MAX_DIGITS];

   

    printf("Enter the base of the input number (2, 8, 10, 16): ");

    scanf("%d", &fromBase);

   

    printf("Enter the number: ");

    scanf("%s", number);

   

    printf("Enter the base to convert to (2, 8, 10, 16): ");

    scanf("%d", &toBase);

 

    if (fromBase == 10)

        decimalToOther(atoi(number), toBase);

    else {

        int decimalValue = otherToDecimal(number, fromBase);

        decimalToOther(decimalValue, toBase);

    }

}

 

void decimalToOther(int number, int base) {

    char result[MAX_DIGITS];

    int index = 0;

 

    if (number == 0) {

        printf("Converted number: 0\\n");

        return;

    }

 

    while (number > 0) {

        int remainder = number % base;

        result[index++] = (remainder < 10) ? remainder + '0' : remainder - 10 + 'A';

        number /= base;

    }

    result[index] = '\\0';

   

    for (int i = 0; i < index / 2; i++) {

        char temp = result[i];

        result[i] = result[index - i - 1];

        result[index - i - 1] = temp;

    }

   

    printf("Converted number: %s\\n", result);

    storeInFile(number, result, 10, base);

}

 

int otherToDecimal(char number[], int base) {

    int length = strlen(number);

    int decimalValue = 0;

    int power = 1;

   

    for (int i = length - 1; i >= 0; i--) {

        int digit;

        if (number[i] >= '0' && number[i] <= '9')

            digit = number[i] - '0';

        else

            digit = number[i] - 'A' + 10;

 

        decimalValue += digit * power;

        power *= base;

    }

    return decimalValue;

}

 

void storeInFile(int input, char output[], int fromBase, int toBase) {

    FILE *file = fopen("conversion_log.txt", "a");

    if (file == NULL) {

        printf("Error opening file!\\n");

        return;

    }

    fprintf(file, "%d (Base %d) -> %s (Base %d)\\n", input, fromBase, output, toBase);

    fclose(file);

}

 

void viewPastConversions() {

    FILE *file = fopen("conversion_log.txt", "r");

    if (file == NULL) {

        printf("No past conversions found!\\n");

        return;

    }

   

    char line[100];

    printf("\\nPast Conversions:\\n");

    while (fgets(line, sizeof(line), file) != NULL) {

        printf("%s", line);

    }

    fclose(file);

}

 


How to Run This Code in Code::Blocks

1️⃣ Install Code::Blocks with MinGW C Compiler.
 2️⃣
Create a New C File (number_conversion.c).
 3️⃣
Paste the Code & Save It.
 4️⃣
Compile & Run (Press F9).
 5️⃣
Interact with the Menu to convert numbers or view past conversions.


Use Cases of This Project

📌 Educational Purpose: Learn C fundamentals, file handling, and base conversion.
 ðŸ“Œ Competitive Coding: Helps in understanding bitwise operations.
 ðŸ“Œ Real-World Applications: Used in embedded systems, networking, and software development.


Conclusion

This Number System Conversion project in C is an excellent academic project for beginners. It enhances logical thinking, file handling skills, and understanding of number systems. 🚀

📌 Download Full Code & More C Projects Below In the eBook link! 👇

 

------------------------

Brief About “C Code Projects for Beginner Students (Ages 8-16)" eBook

Are you a school student aged 8 to 16 with a budding interest in programming, or perhaps looking for a hands-on way to master C language for your academic projects? Look no further! I am thrilled to announce the launch of "C Code Projects for Beginner Students," your ultimate guide to practical C programming.

 

Ready to start your coding adventure?

[Click below any links to get your copy of "C Code Projects for Beginner Students (Ages 8-16)"!]

 

eBook CCP_L01 Link:

https://play.google.com/store/books/details?id=KS54EQAAQBAJ  [Google Play Store]

https://books.google.co.in/books?id=KS54EQAAQBAJ   [Google Books]

 

Enjoy this eBook CCP_L01 on ‘C Code Projects for Beginner Students’ series and do not forget to explore other resources related to this series eBook. Thanks for visiting my blog.

 

EBOOK CCP_L01 promotion Blog Link:

https://myspacemywork2024.blogspot.com/2025/08/unlock-world-of-code-introducing-c-code.html

 

Happy Reading!

…till next post, bye-bye & take care!

No comments:

Post a Comment