5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
int main()
{
int i, j, n=5;
for(i=n; i>1; i--)
{
for(j=n;j>=1;j--)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
for(j=2;j<=n;j++)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
printf("\n");
}
for(i=1; i<=n; i++)
{
for(j=n;j>=1;j--)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
for(j=2;j<=n;j++)
{
if(j>i) printf("%d ", j);
else printf("%d ", i);
}
printf("\n");
}
return 0;
}
Related Links:
- More Number Pattern Programs
- Star Pattern Programs in C
- Alphabet Pattern Programs in C
- Series Programs in C
I still don't get it, can you explain me how the code work?
ReplyDeletedry run it...
Deletetry to dry run the code using n=3
DeleteCan anyone tell me revised version of this pattern
Delete#include
ReplyDeleteint main()
{
int r,i,j,a;
printf("Enter the no. of rows : ");
scanf("%d",&r);
for(i=1;i<=r;i++)
{
for(j=1,a=r;j<=(2*r)-1;j++)
{
if(j>=i && j<=(2*r)-i)
{
printf("%d ",a);
}
else if(j=1;i--)
{
for(j=1,a=r;j<=(2*r)-1;j++)
{
if(j>=i && j<=(2*r)-i)
{
printf("%d ",a);
}
else if(j<i)
{
printf("%d ",a--);
}
else
{
printf("%d ",++a);
}
}
printf("\n");
}
return 0;
}
More Optimized version:
ReplyDeletevoid main()
{
int n=5;
int nr=n*2-1;
int nc=n*2-1;
int i,j,k,ti,tj;
clrscr();
for(i=0;i(nr-1))
{
ti=nr-1-i;
tj=nc-1-j;
}
else
{
ti=i;
tj=j;
}
k=getMin(ti,tj);
if(k<n)
cout<<n-k;
else
cout<<n-(n-k);
}
}
getch();
}
int getMin(int x,int y)
{
if(y<x)
return y;
else
return x;
}
i want opposite of this pattern plz help
ReplyDeletei want this :
111111111
122222221
123333321
123444321
123454321
123444321
123333321
122222221
111111111
#include
Deleteint main()
{
int i, j, n=5;
for(i=1;i<5; i++)
{
for(j=1;j<=5;j++)
{
if(j=1;j--)
{
if(j=1; i--)
{
for(j=1;j<=5;j++)
{
if(j=1;j--)
{
if(j<i) printf("%d ", j);
else printf("%d ", i);
}
printf("\n");
}
return 0;
}
hi can u explain what %d stands for?
Delete#include
ReplyDeleteint main()
{
int i, j, n=5;
for(i=1;i<5; i++)
{
for(j=1;j<=5;j++)
{
if(j=1;j--)
{
if(j=1; i--)
{
for(j=1;j<=5;j++)
{
if(j=1;j--)
{
if(j<i) printf("%d ", j);
else printf("%d ", i);
}
printf("\n");
}
return 0;
}
55555
ReplyDelete54444
54333
54322
54321
#include
ReplyDelete#include
#include
#include
int main()
{
int n;
scanf("%d", &n);
int len=n*2-1;
for(int i=0;i<len;i++){
for(int j=0;j<len;j++)
{
int min= i<j?i:j;
min= min<len-i?min:len-i-1;
min= min<=len-j-1? min:len-j-1;
printf("%d" " ",n-min);
}
printf("\n");
}
return 0;
}
#In python
ReplyDeleten=int(input())
h=(2*n-1)
r=0
q=0
w=0
v=0
a=1
e=0
for i in range(h):
if ir and j=h-1-r and j=n):
print(n-v+w+1,end=" ")
w=w+1
else:
print(n-v+w,end=" ")
w=w+1
else:
print(" ",end=" ")
print()
r=r+1
v=v+1
q=0
w=0
else:
f=i
g=i-r+2
for j in range(h):
if(i==h-1 or j==0 ):
print(n,end=" ")
elif( j=f-a and j=h-1-w-r+2 and j<h):
print(g+e,end=" ")
e=e+1
else:
print(" ",end=" ")
print()
f=f+1
e=0
q=0
w=w-1
g=g+1
a=a+2
555555555
ReplyDelete544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
#include
using namespace std;
int main(){
int i,j,N,count=0,tao;
cin>>N;
i=1;
while(i<=N){
j=1;
count=1;
while(j<=2*N-1){
if(count<=i){
tao=N-j+1;
cout<<tao;
} else if (count<=2*N-i){
cout<<tao;
} else {
cout<<tao+j-(2*N-i);
}
count++;
j++;
}
cout<<endl;
i++;
}
i=1;
while(i<N){
j=1;
count=1;
while(j<=2*N-1){
if(count<=N-i){
tao=N-j+1;
cout<<tao;
} else if (count<=N+i){
cout<<tao;
} else {
cout<<tao+j-(N+i);
}
count++;
j++;
}
cout<<endl;
i++;
}
return 0;
}