jeudi 28 juillet 2022

How to print numerical pattern with for loop in c

I have tried like this but I can print this pattern correctly only for 4. when I gives 5,2 or other numbers as input the code fails... how can i modify this program to match the exact output.. I have searched in many platfroms but cannot got the answer..

 #include <stdio.h>
 #include <string.h>
 #include <math.h>
 #include <stdlib.h>

int main() 
{

   int n;
   scanf("%d", &n);
   // Complete the code to print the pattern.
   int limit=(n*2)-1;
   int row,coloum;

   for( row=1; row<=limit; row++){
      for( coloum=1; coloum<=limit; coloum++){

        if(coloum==1||coloum==7||row==1||row==7) printf("%d ",n);
          else if(coloum==2||coloum==6||row==2||row==6) printf("%d ",n-1);
          else if(coloum==3||coloum==5||row==3||row==5) printf("%d ",n-2);
          else if(coloum==4||coloum==4||row==4||row==4) printf("%d ",n-3);
    
     }
     printf("\n");
   
 }

  return 0;

}

Aucun commentaire:

Enregistrer un commentaire