Certainly! Here’s a C program that converts a number into its equivalent characters:
#include <stdio.h> void convertNumberToCharacters(int number) { if (number == 0) { printf("Zero\n"); return; } int numDigits = 0; int temp = number; while (temp > 0) { temp /= 10; numDigits++; } char *words[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; char result[numDigits + 1]; result[numDigits] = '\0'; for (int i = numDigits - 1; i >= 0; i--) { result[i] = words[number % 10][0]; number /= 10; } printf("%s\n", result); } int main() { int number; printf("Enter a number: "); scanf("%d", &number); convertNumberToCharacters(number); return 0; }
In this program, the function convertNumberToCharacters
takes an integer number
as input and converts it into its equivalent characters. It uses an array words
to store the character representation of each digit from 0 to 9.
The function first checks if the number is 0. If so, it prints “Zero” and returns. Otherwise, it calculates the number of digits in the given number using a while loop.
Then, it creates a character array result
with a size of numDigits + 1
and initializes the last element with the null character to mark the end of the string.
Next, it iterates over the digits of the number from right to left and assigns the corresponding character to the result
array by indexing into the words
array. Finally, it prints the result
array.
In the main
function, the user is prompted to enter a number, which is then passed to the convertNumberToCharacters
function for conversion.
Please note that this program assumes that the entered number is a non-negative integer.