C language: print a rhombus (input the number of rows in the upper half of the rhombus)

topic:

Input the following pattern on the screen in C language:

                    

 =========================================================================

                       

Ideas:

general idea:

(one).

Enter the number of rows in the upper half of the rhombus -- scanf() function

         

(two).

Use a for loop to print the triangle ,

The number of triangle lines in the upper half of the rhombus -- i < line

(Note: It is only the number of rows of triangles in the upper part of the rhombus , and the upper part is a regular triangle )

       

Use the embedded for loop to loop and print out the space :

The upper part of the diamond-shaped space rules :

j < line - 1 - i

Such as : line is equal to 7 , then,

The first line prints 7-1-0=6 spaces ;

The second line prints 7-1-1=5 spaces .

           

Embed the second for loop and print  the * number in the upper half of the diamond :

The rule of the rhombus * in the upper part :

j < 2 * i + 1

Such as : line is equal to 7 , then,

The first line prints 2*0+1=1 * number ;

The second line prints 2*1+1=3 * numbers .

                

After printing a line , wrap it :

printf("\n");

            

(three).

Use the for loop to print the triangle in the lower half of the rhombus ,

Number of rows of triangles in the lower half of the rhombus -- i < line-1

(Note: It is the number of rows of triangles in the lower half of the rhombus , and the lower part is an inverted triangle )

              

Use the embedded for loop to loop and print out the spaces in the lower half of the rhombus :

The lower half of the rhombus space rule :

j <= i

For example : line-1 is 7-1=6 , then,

print the first line

j=0,i=0,j<=i 

1 space ;

print the second line

j=0,i=1,j<=i,

2 spaces .

           

Embed the second for loop and print the * number in the lower half of the diamond  :

The rule of the rhombus * in the lower part :

j < 2*(line-1-i)-1

For example : line-1 is 7-1=6 , then,

The first line prints 2*(7-1-0)-1=11  * numbers ;

The second line prints 2*(7-1-1)-1=9  * numbers .

                

After printing a line , wrap it :

printf("\n");

                 


                 

first step:

Enter the number of rows in the upper half of the rhombus -- scanf() function

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入菱形上半部分行数 -- scanf()函数:
	int line = 0; //上半行数
	//输入:
	scanf("%d", &line);


	return 0;
}

Realize the picture:

                 


                 

Step two:

(1).

Use a for loop to print the triangle ,

The number of triangle lines in the upper half of the rhombus -- i < line

(Note: It is only the number of rows of triangles in the upper part of the rhombus , and the upper part is a regular triangle )

       

(2).

Use the embedded for loop to loop and print out the space :

The upper part of the diamond-shaped space rules :

j < line - 1 - i

Such as : line is equal to 7 , then,

The first line prints 7-1-0=6 spaces ;

The second line prints 7-1-1=5 spaces .

           

(3).

Embed the second for loop and print  the * number in the upper half of the diamond :

The rule of the rhombus * in the upper part :

j < 2 * i + 1

Such as : line is equal to 7 , then,

The first line prints 2*0+1=1 * number ;

The second line prints 2*1+1=3 * numbers .

                

(4).

After printing a line , wrap it :

printf("\n");

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入菱形上半部分行数 -- scanf()函数:
	int line = 0; //上半行数
	//输入:
	scanf("%d", &line);

	//菱形上半部分的打印:行数 -- line
	int i = 0;
	for (i = 0; i < line; i++)
	{
		//打印一行,先打印空格,再打印*号

		//内嵌for循环 打印空格:
		//上半部分空格规律:
		//			line-1-i:假设行数是7,
		//第一行打印7-1-0=6个空格;第二行打印7-1-1=5个空格……
		int j = 0;
		for (j = 0; j < line-1-i; j++)
		{
			printf(" "); //打印空格
		}

		//内嵌第二个for循环 打印*号:
		//上半部分*号规律:
		//			2*i+1:假设行数是7,
		//第一行打印2*0+1个*号;第二行打印2*1+1个*号……
		for (j = 0; j < 2*i+1; j++)
		{
			printf("*"); //打印*号
		}

		//打印完一行后就进行换行:
		printf("\n");
	}

	return 0;
}

Realize the picture:

                 


                 

third step:

(1).

Use the for loop to print the triangle in the lower half of the rhombus ,

Number of rows of triangles in the lower half of the rhombus -- i < line-1

(Note: It is the number of rows of triangles in the lower half of the rhombus , and the lower part is an inverted triangle )

              

(2).

Use the embedded for loop to loop and print out the spaces in the lower half of the rhombus :

The lower half of the rhombus space rule :

j <= i

For example : line-1 is 7-1=6 , then,

print the first line

j=0,i=0,j<=i 

1 space ;

print the second line

j=0,i=1,j<=i,

2 spaces .

           

(3).

Embed the second for loop and print the * number in the lower half of the diamond  :

The rule of the rhombus * in the lower part :

j < 2*(line-1-i)-1

For example : line-1 is 7-1=6 , then,

The first line prints 2*(7-1-0)-1=11  * numbers ;

The second line prints 2*(7-1-1)-1=9  * numbers .

                

(4).

After printing a line , wrap it :

printf("\n");

                     

Implementation code:

#include <stdio.h>
int main()
{
	//输入菱形上半部分行数 -- scanf()函数:
	int line = 0; //上半行数
	//输入:
	scanf("%d", &line);

	//菱形上半部分的打印:行数 -- line
	int i = 0;
	for (i = 0; i < line; i++)
	{
		//打印一行,先打印空格,再打印*号

		//内嵌for循环 打印空格:
		//上半部分空格规律:
		//			line-1-i:假设行数是7,
		//第一行打印7-1-0=6个空格;第二行打印7-1-1=5个空格……
		int j = 0;
		for (j = 0; j < line-1-i; j++)
		{
			printf(" "); //打印空格
		}

		//内嵌第二个for循环 打印*号:
		//上半部分*号规律:
		//			2*i+1:假设行数是7,
		//第一行打印2*0+1个*号;第二行打印2*1+1个*号……
		for (j = 0; j < 2*i+1; j++)
		{
			printf("*"); //打印*号
		}

		//打印完一行后就进行换行:
		printf("\n");
	}

	//菱形下半部分的打印:行数 -- line-1
	for (i = 0; i < line-1; i++)
	{
		//打印一行,先打印空格,再打印*号
		
		//内嵌for循环 打印空格:
		//下半部分空格规律:
		//			j<=i:假设行数是7-1=6,
		//第一行打印j=0,i=0,j<=i,1个空格;第二行打印j=0,i=1,j<=i,2个空格……
		int j = 0;
		for (j = 0; j <= i; j++)
		{
			printf(" "); //打印空格
		}

		//内嵌第二个for循环 打印*号:
		//下半部分*号规律:
		//		2*(line-1-i)-1:假设行数是7-1=6,
		//第一行打印2*(7-1-0)-1,11个*号;第二行打印2*(7-1-1)-1,9个*号……
		for (j = 0; j < 2*(line-1-i)-1; j++)
		{
			printf("*"); //打印*号
		}

		//打印完一行后就进行换行:
		printf("\n");
	}

	return 0;
}

Realize the picture:

                    

Final code and implementation effect

Final code:

#include <stdio.h>
int main()
{
	//输入菱形上半部分行数 -- scanf()函数:
	int line = 0; //上半行数
	//输入:
	scanf("%d", &line);

	//菱形上半部分的打印:行数 -- line
	int i = 0;
	for (i = 0; i < line; i++)
	{
		//打印一行,先打印空格,再打印*号

		//内嵌for循环 打印空格:
		//上半部分空格规律:
		//			line-1-i:假设行数是7,
		//第一行打印7-1-0=6个空格;第二行打印7-1-1=5个空格……
		int j = 0;
		for (j = 0; j < line-1-i; j++)
		{
			printf(" "); //打印空格
		}

		//内嵌第二个for循环 打印*号:
		//上半部分*号规律:
		//			2*i+1:假设行数是7,
		//第一行打印2*0+1个*号;第二行打印2*1+1个*号……
		for (j = 0; j < 2*i+1; j++)
		{
			printf("*"); //打印*号
		}

		//打印完一行后就进行换行:
		printf("\n");
	}

	//菱形下半部分的打印:行数 -- line-1
	for (i = 0; i < line-1; i++)
	{
		//打印一行,先打印空格,再打印*号
		
		//内嵌for循环 打印空格:
		//下半部分空格规律:
		//			j<=i:假设行数是7-1=6,
		//第一行打印j=0,i=0,j<=i,1个空格;第二行打印j=0,i=1,j<=i,2个空格……
		int j = 0;
		for (j = 0; j <= i; j++)
		{
			printf(" "); //打印空格
		}

		//内嵌第二个for循环 打印*号:
		//下半部分*号规律:
		//		2*(line-1-i)-1:假设行数是7-1=6,
		//第一行打印2*(7-1-0)-1,11个*号;第二行打印2*(7-1-1)-1,9个*号……
		for (j = 0; j < 2*(line-1-i)-1; j++)
		{
			printf("*"); //打印*号
		}

		//打印完一行后就进行换行:
		printf("\n");
	}

	return 0;
}

Realize the effect:

Guess you like

Origin blog.csdn.net/weixin_63176266/article/details/131367739
Recommended