问题
I have to print the following pattern using for loop only.
I have written the following code which gives me till the the numbers increment. How can I decrement then in the other half.
#include<stdio.h>
int main(void)
{
int x,r;
for(r=1;r<5;r++)
{
for(x=3;x>=r;x--)
{
printf(" ");
}
for(x=1;x<=r;x++)
{
printf("%d",x);
}
printf("\n");
}
return 0;
}
回答1:
just add a second loop from r-1 to 1?
for (x = r - 1; x > 0; x--)
{
printf("%d",x);
}
回答2:
Added one more loop
for(x=1;x<=(r-1);x++)
{
printf("%d",x);
}
whole code.
#include<stdio.h>
int main(void)
{
int x,r;
for(r=1;r<5;r++)
{
for(x=3;x>=r;x--)
{
printf(" ");
}
for(x=1;x<=r;x++)
{
printf("%d",x);
}
for(x=1;x<=(r-1);x++)
{
printf("%d",x);
}
printf("\n");
}
return 0;}
code running at http://codepad.org/oAdx20ai
来源:https://stackoverflow.com/questions/26277252/pattern-using-for-loop