POP Lab Manual

Program 4

Write a C Program to display a Number Pattern by reading the number of rows as input. The pattern creates a symmetric pyramid where each row contains numbers from 1 to row number and then back down to 1, creating a beautiful diamond-like structure.
Pattern Structure (for n=4):

1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1

Algorithm

  1. Initialize: Start the program and declare variables n, i, j
  2. Input Rows: Read the number of rows (n) from user
  3. Outer Loop: For each row i from 1 to n
  4. Print Spaces: Print (n-i) spaces for proper alignment
  5. Ascending Numbers: Print numbers from 1 to i with spaces
  6. Descending Numbers: Print numbers from (i-1) down to 1 with spaces
  7. New Line: Move to next line after completing each row
  8. Terminate: Stop the program after all rows are printed

Flowchart

START
Input n
i = 1 to n
Print Spaces
Print 1 to i
Print i-1 to 1
New Line
STOP

Code

#include <stdio.h>


int main() {
    int n, i, j;

    printf("Enter number of rows: ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++) {
        // Print spaces
        for (j = 1; j <= n - i; j++) {
            printf("  ");
        }

        // Print increasing numbers
        for (j = 1; j <= i; j++) {
            printf("%d ", j);
        }

        // Print decreasing numbers
        for (j = i - 1; j >= 1; j--) {
            printf("%d ", j);
        }

        printf("\n");
    }

    return 0;
}

Sample Output


// Example 1 - For n = 4
Enter number of rows: 4
      1 
    1 2 1 
  1 2 3 2 1 
1 2 3 4 3 2 1 

// Example 2 - For n = 5
Enter number of rows: 5
        1 
      1 2 1 
    1 2 3 2 1 
  1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 

// Example 3 - For n = 6
Enter number of rows: 6
          1 
        1 2 1 
      1 2 3 2 1 
    1 2 3 4 3 2 1 
  1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 

// Example 4 - For n = 3
Enter number of rows: 3
    1 
  1 2 1 
1 2 3 2 1 

          
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.