C - Program - Convert Number to Text

To convert a number into its equivalent characters in C, you can use a combination of arithmetic operations and character handling functions. 

#include <stdio.h>
void convertToCharacters(int number) {
    // Determine the number of digits in the input number
    int numDigits = 0;
    int temp = number;
    while (temp != 0) {
        temp /= 10;
        numDigits++;
    }
    // Create an array to store the characters
    char characters[numDigits + 1];
    // Convert each digit to its character representation
    for (int i = numDigits - 1; i >= 0; i--) {
        characters[i] = (number % 10) + '0';
        number /= 10;
    }
    characters[numDigits] = '\0'; // Null-terminate the character array
    // Print the character representation
    printf("Character representation: %s\n", characters);
}
int main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);
    convertToCharacters(number);
    return 0;
}

In this program, the function convertToCharacters() takes an integer number as input and converts it into its character representation. It first determines the number of digits in the input number by repeatedly dividing it by 10 and counting the iterations. Then, it creates a character array of appropriate size to store the characters.

Next, it uses a loop to convert each digit of the number to its character representation by adding the ASCII value of '0'. This is achieved by performing modulo division (number % 10) to extract the last digit and then dividing the number by 10 to remove the last digit. The resulting character is stored in the character array in reverse order.

Finally, the character array is null-terminated (characters[numDigits] = '\0') to mark the end of the string, and the character representation is printed using printf().