HackerRank C- Printing Pattern using Loops




In this problem, you need to print the pattern of the following form containing the numbers from 1 to n.



  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. int main()
  6. {
  7. int n;
  8. scanf("%d", &n);
  9. int len = n*2 - 1;
  10. for(int i=0;i<len;i++){
  11. for(int j=0;j<len;j++){
  12. int min = i < j ? i : j;
  13. min = min < len-i ? min : len-i-1;
  14. min = min < len-j-1 ? min : len-j-1;
  15. printf("%d ", n-min);
  16. }
  17. printf("\n");
  18. }
  19. return 0;
  20. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube