C Online Compiler
Example: Custom Character Sales Bar Chart in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Custom Character Sales Bar Chart #include <stdio.h> #include <string.h> // Required for strlen in some scenarios, though not strictly here for basic char int main() { // Step 1: Define product names and sales data char *products[] = {"Quarter 1", "Quarter 2", "Quarter 3", "Quarter 4"}; int sales[] = {25000, 32000, 18000, 40000}; int num_products = sizeof(sales) / sizeof(sales[0]); // Step 2: Define custom bar character and legend char bar_char = '#'; int unit_per_char = 1000; // Each '#' represents 1000 units of sales // Step 3: Print Chart Header and Legend printf("--- Sales Performance by Quarter ---\n"); printf("Legend: Each '%c' represents %d units of sales\n", bar_char, unit_per_char); printf("-------------------------------------\n"); // Step 4: Iterate through products and print bars for (int i = 0; i < num_products; i++) { printf("%-12s | ", products[i]); // Print quarter name // Step 5: Calculate number of bar_chars and print int bar_length = sales[i] / unit_per_char; for (int j = 0; j < bar_length; j++) { printf("%c", bar_char); } printf(" (%d)\n", sales[i]); // Print actual sales value } printf("-------------------------------------\n"); return 0; }
Output
Clear
ADVERTISEMENTS