the str in line 1 almost drives me crazy.fortunately,adding 'else'in functin has solve the problem #include<stdio.h> int main(){ int x; while(scanf("%d",&x)!=EOF){ for(int i=1;i<=x;i++){ for(int j=1;j<=x;j++){ if(j==x) printf("\n"); else if(x-j<i&&x-j>0) printf(" "); else printf(" "); } } } }