Printing Pattern using Loops | HackerRank - Samsus Salehin Sabbir

Header Ads

Printing Pattern using Loops | HackerRank


Problem Link : Printing Pattern using Loops

Solution :
      Develop by
 Samsus Salehin Sabbir 

  1.  #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>

  5. #include <stdio.h>

  6. int main() 
  7. {

  8.     int n;
  9.     scanf("%d", &n);
  10.     int len = n*2 - 1; // if n=2 than 2*2-1 = 4-1=3 
  11.     for(int i=0;i<len;i++){
  12.         for(int j=0;j<len;j++){
  13.             int min = i < j ? i : j;
  14.             min = min < len-i ? min : len-i-1;
  15.             min = min < len-j-1 ? min : len-j-1;
  16.             printf("%d ", n-min);
  17.         }
  18.         printf("\n");
  19.     }
  20.     return 0;
  21. }

No comments

Powered by Blogger.