GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

print the pattern 1 121 12321 1234321

asked Mar 11, 2016 by anonymous

7 Answers

/* The following solution is in Python. Here , i get the n value from the user and generate the pattern for n times. */ 

n = input()
L = []
for i in range(1,n+1):
    L.append(str(i))
    print ''.join(L)+''.join(L[::-1][1:])

 

Sample Input 1:

5

Sample output 1:

1
121
12321
1234321
123454321

Sample Input 2:

15

Sample Output 2:

1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
12345678910987654321
123456789101110987654321
1234567891011121110987654321
12345678910111213121110987654321
123456789101112131413121110987654321
1234567891011121314151413121110987654321

 

answered Mar 12, 2016 by Sathish Kumar 1

#include<iostream>
using namespace std;
int main()
{
    int i,j,k,t,n,c,a[20];
    cin>>t;
    for(k=0;k<20;k++)
        a[k]=(k+1);
    while(t--)
    {
        cin>>n;
        for(i=0;i<n;i++)
        {
            c=i-1;
            for(j=0;j<(2*i+1);j++)
            {
                if(i>=j)
                    cout<<a[j];
                else
                {
                    cout<<a[c];
                    c--;
                }
            }cout<<" ";
        }cout<<endl;
    }
    return 0;
}

answered Sep 7, 2016 by Rohit Ranjan 1
import java.util.Scanner; public class pattern { public static void main(String...a) { int n,i,j=1,k,l,m; System.out.println("enter the number"); Scanner s=new Scanner(System.in); n=s.nextInt(); if(n!=0) System.out.println(1); for(i=2;i<=n;i++) { j=j*10+i; k=j; m=j; m=m/10; while(m>0) { l=m%10; k=k*10+l; m=m/10; } System.out.println(k); } } }
answered Sep 8, 2016 by 95peeyush jain 1 flag

#include<stdio.h>
int main()
{
int i,j,n,k;
printf("enter the number :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=n;k>i;k--)printf(" ");
for(j=1;j<=i;j++)
{
printf("%d",j);
}
if(i>1)
{
for(j=i;j>1;j--)
{
printf("%d",j-1);
}
}
printf("\n");
}
return 0;
}

answered Oct 13, 2016 by Rishu

#include<stdio.h>
void main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        printf("%d",j);
        for(j=i-1;j>=1;j--)
        printf("%d",j);
        printf("\n");
    }

}

answered Oct 14, 2016 by akhilmohan_kanan

Simple Python solution for small inputs using pure maths!!!

for i in range(1,int(raw_input())+1):

    print pow(((pow(10,i)-1)/9),2)

answered Oct 20, 2016 by gsomani
#include <iostream>
using namespace std;

int main() {
	int n = 4;
	for(int i=1;i <=n ;i++){
		for(int j=1;j <=i ;j++)
			cout << j;
		for(int j=i-1;j >=1 ;j--)
			cout << j;
		cout<< endl;
	}
	return 0;
}

 

answered Mar 11, 2016 by aditya.goel
...