Monday, September 5, 2016

Special number series pattern - 1


/*C program to print special number series pattern-1.
For n=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
*/
#include<stdio.h>
int main()
{
     int i, j, k, n;
     printf("Enter your number:");
     scanf("%d", &n);
     for (i=0; i<n; i++)
     {
           //logic for intial spaces
           for(j=0; j<=n-i; j++)
           {
                printf("  ");
           }
           //logic for priting actual numbers
           for(k=0; k<=i; k++)
                printf("%d ", k+1);

           for(j=i; j>0; j--)
                printf("%d ", j);

           printf("\n");
     }
     return 0; 
}


Output of the Program:

Enter your number: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


No comments:

Post a Comment