A Number Pattern Program in C is a type of program that generates and displays a specific pattern of numbers based on user-defined input. These patterns can take various forms, such as triangles, squares, or pyramids, and are often used to demonstrate nested loops and formatting in C.

Example: Right-Angled Triangle Number Pattern

Let's explain a simple program that prints a right-angled triangle number pattern. For instance, if the user inputs the number 5, the output will look like this:

1 12 123 1234 12345

Logic of the Program:

  1. Take an integer input from the user that represents the number of rows for the pattern.
  2. Use a nested loop:
    • The outer loop will iterate through each row.
    • The inner loop will iterate through numbers from 1 to the current row number.
  3. Print the numbers in the inner loop.
  4. After each row, print a newline character to move to the next line.

C Program to Generate a Right-Angled Triangle Number Pattern:

#include <stdio.h> int main() { int rows, i, j; // Input the number of rows from the user printf("Enter the number of rows: "); scanf("%d", &rows); // Generate the number pattern for (i = 1; i <= rows; i++) { // Outer loop for each row for (j = 1; j <= i; j++) { // Inner loop for numbers in each row printf("%d", j); // Print the number } printf("\n"); // Move to the next line after each row } return 0; }

Explanation:

  1. Variables:

    • int rows: Holds the number of rows for the pattern.
    • int i, j: Loop counters.
  2. Input:

    • The program prompts the user to enter the number of rows for the pattern.
  3. Outer Loop:

    • The outer loop (for (i = 1; i <= rows; i++)) controls the number of rows. It runs from 1 to rows.
  4. Inner Loop:

    • The inner loop (for (j = 1; j <= i; j++)) controls the numbers printed on each row. It runs from 1 to i, ensuring that the number of printed digits increases with each row.
  5. Printing:

    • Inside the inner loop, the program prints the current value of j.
    • After the inner loop, a newline (printf("\n")) is printed to move to the next row.
  6. Output:

    • Finally, the program displays the number pattern based on the user's input.

Sample Output:

Example 1:

Enter the number of rows: 5 1 12 123 1234 12345

Example 2:

Enter the number of rows: 3 1 12 123

Key Points:

  • Nested Loops: The program demonstrates the use of nested loops, which are essential for generating patterns.
  • Dynamic Input: The number of rows can be changed by the user, making the program flexible.
  • Simple Logic: The program has a straightforward logic that can be easily modified to create different patterns, such as pyramids, inverted triangles, etc.