用C语言画三角形
2016-08-05 09:19 yojiaku 阅读(3861) 评论(0) 编辑 收藏 举报【最简单】
/*
Write a program in C to display the pattern like right angle triangle using asterisk.
*/
#include <stdio.h> int main() { int i, j, rows; printf("Input number of rows:"); scanf("%d",&rows); for(i = 1;i <= rows;i++) { for(j = 1;j <= i;j++) { printf("*"); } printf("\n"); } return 0; }
计算流程图:
输出结果:
【升级版】
/* Write a program in C to make such a pattern like right angle triangle with number which will repeat a number in a row. */ #include <stdio.h> int main() { int i, j, rows; printf("Input number of rows:"); scanf("%d",&rows); for(i = 1;i <= rows;i++) { for(j = 1;j <= i;j++) { printf("%d",j); } printf("\n"); } return 0; }
输出结果:
【升级】
/* Write a program in C to make such a pattern like right angle triangle with number which will repeat a number in a row. */ #include <stdio.h> int main() { int i, j, rows; printf("Input number of rows:"); scanf("%d",&rows); for(i = 1;i <= rows;i++) { for(j = 1;j <= i;j++) { printf("%d",i); } printf("\n"); } return 0; }
输出结果:
【再升级】
/* Write a program in C to make such a pattern like right angle triangle with number increased by 1. */ #include <stdio.h> int main() { int i, j, rows, k = 1; printf("Input number of rows:"); scanf("%d",&rows); for(i = 1;i <= rows;i++) { for(j = 1;j <= i;j++) { printf("%d",k++); } printf("\n"); } return 0; }
流程图:
打印结果:
【升级】
/* Write a program in C to make such a pattern like pyramid with numbers increased by 1. */ #include <stdio.h> int main() { int i,j,spc,rows,k,t=1; printf("input number of rows : "); scanf("%d",&rows); spc=rows+4-1; for(i=1;i<=rows;i++) { for(k=spc;k>=1;k--) { printf(" "); } for(j=1;j<=i;j++){ printf("%d ",t++); } printf("\n"); spc--; } return 0; }
流程图:
打印结果:
【升级】
/* Write a program in C to make such a pattern like pyramid with asterisk. */ #include <stdio.h> int main() { int i,j,spc,rows,k; printf("input number of rows : "); scanf("%d",&rows); spc=rows+4-1; for(i=1;i<=rows;i++) { for(k=spc;k>=1;k--) { printf(" "); } for(j=1;j<=i;j++){ printf("* "); } printf("\n"); spc--; }
return 0;
}
打印结果: