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.
Hmmm, IMHO the tests suit machinery use to validate output (in the assert
stage), could be improved using patterns, to discard whites character, specially
if we use format string to justify the out put it fails.
printf ("%5d", np);
#include<stdio.h>/** * Given @param n as natural number it prints the following pattern: * * |--------- 2n - 1 ----------| ^ * * n n n ... n n | * n n-1 n-1 ... n-1 n | * n n-1 n-2 ... n-2 n-1 n | * ... * n n-1 n-2 .. 1 .. n-2 n-1 n 2n - 1 * ... * n n-1 n-2 ... n-2 n-1 n | * ... | * n n-1 n-1 ... n-1 n | * n n n ... n n v * * Please note that dimession of "square" is giving by the expression: `2n - 1'. * * I used a Cartesian coordinates to know which number will be located at which * position, incrementing or decrement in function of the coordinate (r,c). */voidprint_pattern(intn){intr,c;if(n<=0){fprintf(stderr,"%s: error: invalid parameter\n",__func__);return;}for(r=0;r<2*n-1;++r){intnp=n;for(c=0;c<2*n-1;++c){printf("%5d",np);if(c<r)np--;if(c+r>=2*n-2)np++;}puts("");}}intmain(){intn;if(scanf("%d",&n)!=1){fprintf(stderr,"error: invalid input\n");return1;}print_pattern(n);return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Printing Pattern Using Loops
You are viewing a single comment's thread. Return to all comments →
Hmmm, IMHO the tests suit machinery use to validate output (in the assert stage), could be improved using patterns, to discard whites character, specially if we use format string to justify the out put it fails.
printf ("%5d", np);