当前位置:优学网  >  在线题库

如何在c中使用for循环打印数字图案

发表时间:2022-07-29 00:00:31 阅读:71

我试过这样做,但我只能正确打印这个模式4.当我输入5,2或其他数字时,代码失败了……我如何修改这个程序以匹配准确的输出……我在许多平台上搜索过,但没有得到答案..

 #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;

}

🎖️ 优质答案
相关问题