Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: numberpattern.c
- Copyright:
- Author: Mr.Kindle (youtube: Mr_Kindle)
- Date: 30-11-22 23:13
- Description: this code print following number of patterns if number of rows = 4.
- 1
- 212
- 32123
- 4321234
- 32123
- 212
- 1
- */
- #include <iostream>
- using namespace std;
- int main()
- {
- int n, c, k,j,flag;
- cout << "Enter number of rows (less than 9 for better result)\n";
- cin >> n;
- /*First make upper triangle*/
- for (k=1; k<=n; k++)//
- { /*for printing the spaces*/
- for (c=1; c<=n-k; c++)
- printf(" ");
- j=k;// so that the value of 'k' doesnot alter.
- flag=0;
- /*for printing the numbers*/
- /*there are '2k - 1' number of element in each 'kth' row.*/
- for (c = 1; c <= 2*k-1; c++)
- { if(j==1)
- { printf("%d",j++);
- flag++;//flag indicates that value 1 has reached
- }
- else if (flag != 0)//once we get 1 after that increased value should be printed
- printf("%d",j++);
- else //value should be printed in descending order till we get 1;
- printf("%d",j--);
- }
- printf("\n");
- }//for loop for upper triangle
- /*making lower inverse triangle*/
- for (k=1; k<=n-1; k++)//we will print 1 line less hence 'k<=n-1' instead of 'k<=n.
- {
- for (c=1; c<=k; c++)
- printf(" ");
- j=n-k;
- flag=0;
- for (c=1 ; c<=2*(n-k)-1; c++)
- {
- if(j==1)
- { printf("%d",j++);
- flag++;
- }
- else if (flag!=0)
- printf("%d",j++);
- else
- printf("%d",j--);
- }
- printf("\n");
- }
- return 0;
- }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement