How to create numbers table?

To create a numbers table in C, you can use a simple loop to print the numbers in a formatted way. Here's an example of how to create a multiplication table for numbers 1 to 10:

#include <stdio.h>

int main() {
    int i, j;

    // Print the header
    printf("Multiplication Table\n");
    printf("---------------------\n");

    // Loop through numbers 1 to 10
    for (i = 1; i <= 10; i++) {
        for (j = 1; j <= 10; j++) {
            printf("%4d", i * j); // Print the product with formatting
        }
        printf("\n"); // New line after each row
    }

    return 0;
}

Steps to Compile and Run:

  1. Create a file named main.c:

    cd ~/project
    touch main.c
    
  2. Copy the above code into main.c.

  3. Compile the program:

    gcc main.c -o main
    
  4. Run the program:

    ./main
    

This will display a multiplication table from 1 to 10. You can modify the range by changing the loop conditions.

0 Comments

no data
Be the first to share your comment!