C程序 打印半直角号码金字塔图案
在这里,我们将看到一个C语言程序来打印3种不同的数字模式。使用for循环和while循环涵盖了3种数字模式及其各自的解释。
3种不同的数字模式。
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
模式1:
输入:
n = 5
输出:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A.使用for循环
第一个for循环用来迭代行的数量,第二个for循环用来重复列的数量。然后打印数字,并递增数字以打印下一个数字。
// C program to print number pyramid patterns using for loop
#include <stdio.h>
int main()
{
int rows, columns, number = 1, n = 5;
// first for loop is used to identify number of rows
for (rows = 0; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 0; columns < rows; columns++) {
// printing number pattern based on the number
// of columns
printf("%d ", number);
// incrementing number at each column to print
// the next number
number++;
}
// print the next line for each row
printf("\n");
}
return 0;
}
输出
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
B.使用while循环
while 循环检查条件,直到条件为假。如果条件为真,则进入循环并执行这些语句。
// C program to print number
// patterns using while loop
#include <stdio.h>
int main()
{
int rows = 1, columns = 0, n = 5;
// 1 value is assigned to the number
// helpful to print the number pattern
int number = 1;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
printf("%d ", number);
// incrementing columns value
columns++;
// incrementing number value to
// print the next number
number++;
}
columns = 0;
// incrementing rows value
rows++;
printf("\n");
}
return 0;
}
输出
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
模式2:
输入:
n = 5
输出:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
A.使用for循环
第一个for循环用于遍历行数,第二个for循环用于重复列数。然后打印行数以获得所需的输出。
// C program to print number
// patterns using for loop
#include <stdio.h>
int main()
{
int rows, columns, n = 5;
// first for loop is used to
// identify number of rows
for (rows = 1; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 1; columns <= rows; columns++) {
// printing number pattern based on the number
// of rows
printf("%d ", rows);
}
// print the next line for each row
printf("\n");
}
return 0;
}
输出
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
B.使用while循环
while 循环检查条件,直到条件为假。如果条件为真,则进入循环并执行这些语句。
// C program to print number
// patterns using while loop
#include <stdio.h>
int main()
{
int rows = 1, columns = 0, n = 5;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
printf("%d ", rows);
// incrementing columns value
columns++;
}
columns = 0;
// incrementing rows value
rows++;
printf("\n");
}
return 0;
}
输出
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
模式3:
输入:
n = 5
输出:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
A.使用for循环
第一个for循环用来迭代行的数量,第二个for循环用来重复列的数量。然后打印列数以获得所需的输出。
// C program to print number
// patterns using while loop
#include <stdio.h>
int main()
{
int rows, columns, n = 5;
// second for loop is used to identify number of rows
for (rows = 1; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 1; columns <= rows; columns++) {
// print the number to be print based on the
// number of columns
printf("%d ", columns);
}
// print the next line
printf("\n");
}
return 0;
}
输出
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
B.使用while循环
while 循环检查条件,直到条件为假。如果条件为真,则进入一个循环并执行这些语句。
// C program to print number
// patterns using while loop
#include <stdio.h>
int main()
{
int rows = 1, columns = 1, n = 6;
int number = 1;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
printf("%d ", columns);
// incrementing columns value
columns++;
}
columns = 1;
// incrementing rows value
rows++;
printf("\n");
}
return 0;
}
输出
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
时间的复杂性: 对于给定的输入n,O(n 2)。
辅助空间: O(1)