
在这里,为了以菱形图案打印星星,我们使用嵌套的 for 循环。
我们用于以菱形图案打印星星的逻辑如下所示 -
//For upper half of the diamond the logic is:
for (j = 1; j <= rows; j++){
for (i = 1; i <= rows-j; i++)
printf(" ");
for (i = 1; i<= 2*j-1; i++)
printf("*");
printf("</p><p>");
}假设让我们考虑 rows=5,它打印输出如下 -
*
***
*****
*******
*********立即学习“C语言免费学习笔记(深入)”;
立即学习“C语言免费学习笔记(深入)”;
//For lower half of the diamond the logic is:
for (j = 1; j <= rows - 1; j++){
for (i = 1; i <= j; i++)
printf(" ");
for (i = 1 ; i <= 2*(rows-j)-1; i++)
printf("*");
printf("</p><p>");
}假设 row=5,将打印以下输出 -
******* ***** *** *
#include <stdio.h>
int main(){
int rows, i, j;
printf("Enter no of rows</p><p>");
scanf("%d", &rows);
for (j = 1; j <= rows; j++){
for (i = 1; i <= rows-j; i++)
printf(" ");
for (i = 1; i<= 2*j-1; i++)
printf("*");
printf("</p><p>");
}
for (j = 1; j <= rows - 1; j++){
for (i = 1; i <= j; i++)
printf(" ");
for (i = 1 ; i <= 2*(rows-j)-1; i++)
printf("*");
printf("</p><p>");
}
return 0;
}Enter no of rows
5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*以上就是如何使用C语言打印出菱形图案中的星星?的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号