We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Printing Pattern Using Loops
Printing Pattern Using Loops
+ 0 comments int main() { int n; scanf("%d", &n); int arr[2*n-1][2*n-1]; // initialize all the array with n // then replace everything except for the outer ring with n - 1 ... // until reaching 1 for (int j = 0; j < n; j++){ for (int i = j; i <= 2*n - 2 - j; i++){ for (int k = j; k <= 2*n - 2 - j; k++){ arr[i][k] = n - j; } } } // print the array for (int i = 0; i < 2*n - 1; i++){ for (int j = 0; j < 2*n -1; j++){ printf("%d ", arr[i][j]); } printf("\n"); } return 0; }
+ 0 comments include
include
include
include
int main() {
int n; scanf("%d", &n); // Complete the code to print the pattern. int i,j,x; for (i = 0; i < 2 *n - 1; i++) { for (j = 0; j < 2 * n - 1; j++) { x = (i < j) ? i : j; x = (x < (2 * n - 1) - i) ? x : (2 * n - 2) - i; x = (x < (2 * n - 1) - j) ? x : (2 * n - 2) - j; printf("%d ", n - x); } printf("\n"); } return 0;
}
+ 0 comments include
include
include
include
int main() {
int n,i,j,k; scanf("%d", &n); for(i =0; i <(2*n-1); i++){ k = n; for(j =0; j<(2*n-1); j++){ if(i>=j && ((2*n-2)-i)>=j){ k=n-j; printf("%d ", k); }else if(i>=((2*n-1)-j) && j>=(i+1)){ k += 1; printf("%d ", k); }else{ printf("%d ", k); } } printf("\n"); } return 0;
}
+ 0 comments include
include
int main() { int n,a; scanf("%d",&n); a=n;
for(int i=1;i<=2*n-1;i++) { for(int j=1;j<=2*n-1;j++) { if(j<n-abs(n-i)){printf("%d ",a);a--;} else if(j>n+abs(n-i)){a++;printf("%d ",a);} else printf("%d ",a); } printf("\n"); }
}
+ 1 comment include
include
include
include
int main() {
int n,num=0;
scanf("%d", &n);
for(int i=0;i<(2*n)-1;i++) {
if(i<=n-1){ for(int j=1, t=0, s=0;j<=(2*n)-1;j++,s++) { if(j<=n){ num=n-t; if(i!=0&&t<i){t++;} printf("%d ",num); } if(j>n){ if(s>=(2*n)-(i+1)){num++;} printf("%d ",num); } } } if(i>=n){ for(int j=1, t=0, s=0;j<=(2*n)-1;j++,s++) { if(j<=n+1){ num=n-t; if(i!=0&&t<(2*n-i-2)){t++;} printf("%d ",num); } if(j>n+1){ if(s>=i+1&&num<n){num++;} printf("%d ",num); } } } printf("\n");
}
return 0; }
Load more conversations
Sort 832 Discussions, By:
Please Login in order to post a comment