Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program that receives a number and prints it out in large size
In C, we can create a program that displays numbers in large size using ASCII art patterns made with hash symbols. This technique is useful for creating visually appealing banner-style output or digital displays.
Syntax
void printLargeNumber(char number[]);
Approach
The solution uses predefined character arrays to store ASCII art patterns for each digit (0-9). Each digit pattern is 7 rows high and 8 characters wide −
- Define 2D character arrays for each digit's visual pattern
- Traverse the input string character by character
- Print corresponding patterns row by row to display digits side by side
Example
Here's a complete program that prints numbers in large size using hash symbols −
#include <stdio.h>
#include <string.h>
#define H 7
#define W 8
void printLargeNumber(char num[]) {
int i, j, k;
/* Predefined patterns for each digit */
char zero[H][W] = {" ##### ", " # # ", " # # ", " # # ", " # # ", " # # ", " ##### "};
char one[H][W] = {" # ", " ## ", " # ", " # ", " # ", " # ", " ##### "};
char two[H][W] = {" ##### ", " # ", " # ", " ##### ", " # ", " # ", " ##### "};
char three[H][W] = {" ##### ", " # ", " # ", " ##### ", " # ", " # ", " ##### "};
char four[H][W] = {" # # ", " # # ", " # # ", " ##### ", " # ", " # ", " # "};
char five[H][W] = {" ##### ", " # ", " # ", " ##### ", " # ", " # ", " ##### "};
char six[H][W] = {" ##### ", " # ", " # ", " ##### ", " # # ", " # # ", " ##### "};
char seven[H][W] = {" ##### ", " # ", " # ", " # ", " # ", " # ", " # "};
char eight[H][W] = {" ##### ", " # # ", " # # ", " ##### ", " # # ", " # # ", " ##### "};
char nine[H][W] = {" ##### ", " # # ", " # # ", " ##### ", " # ", " # ", " ##### "};
if (strlen(num) > 10) {
printf("\nYou must enter a number up to 10 digits.<br>");
return;
}
printf("<br>");
/* Print each row of all digits */
for (j = 0; j < H; j++) {
for (i = 0; i < strlen(num); i++) {
switch (num[i]) {
case '0': printf("%s", zero[j]); break;
case '1': printf("%s", one[j]); break;
case '2': printf("%s", two[j]); break;
case '3': printf("%s", three[j]); break;
case '4': printf("%s", four[j]); break;
case '5': printf("%s", five[j]); break;
case '6': printf("%s", six[j]); break;
case '7': printf("%s", seven[j]); break;
case '8': printf("%s", eight[j]); break;
case '9': printf("%s", nine[j]); break;
default: printf(" "); break;
}
}
printf("<br>");
}
}
int main() {
char number[] = "2168";
printf("Large size representation of %s:", number);
printLargeNumber(number);
return 0;
}
Output
Large size representation of 2168:
##### # ##### #####
# ## # # #
# # # # #
##### # ##### #####
# # # # # #
# # # # # #
##### ##### ##### #####
How It Works
- Pattern Definition: Each digit (0-9) is stored as a 7x8 character array with hash symbols forming the digit shape
- Row-wise Printing: The outer loop iterates through each row (0 to 6), while the inner loop processes each character in the input string
- Character Mapping: A switch statement maps each input character to its corresponding pattern array
- Side-by-side Display: All digits are printed horizontally by processing the same row of each digit before moving to the next row
Key Points
- The program supports numbers up to 10 digits in length
- Each digit pattern is exactly 7 rows high and 8 characters wide for consistency
- Uses a switch statement for cleaner code compared to multiple if-else conditions
- The outer loop controls the row, inner loop controls the digit position
Conclusion
This C program effectively creates large ASCII art representations of numbers using predefined character patterns. It demonstrates practical use of 2D arrays and string manipulation for visual output formatting.
Advertisements
