# 期中考
```cpp=
#include <stdio.h>
#include <stdbool.h>
int main()
{
printf("Please choose which pattern you want to print: (Enter [1/2/3/4])\n");
printf("1: Symmetry pattern\n");
printf("2: Number triangle\n");
printf("3: Square pattern with stars\n");
printf("4: End program\n");
int mode, n;
scanf("%d", &mode);
while (mode != 4)
{
if (mode == 1)
{
printf("Please enter the length of number sequence: ");
scanf("%d", &n);
// check whether the input number is within the range
while (n <= 0)
{
printf("The size has to be positive. Please enter again: ");
scanf("%d", &n);
}
// output the number sequence using nested loop
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < n; j++)
{
if (j <= i)
printf("%d ", j);
else
printf(" ");
}
if (i == n)
printf("%d ", n);
else
printf(" ");
for (int j = n - 1; j > 0; j--)
{
if (j <= i)
printf("%d ", j);
else
printf(" ");
}
printf("\n");
}
printf("Please choose which pattern you want to print: (Enter [1/2/3/4])\n");
printf("1: Symmetry pattern\n");
printf("2: Number triangle\n");
printf("3: Square pattern with stars\n");
printf("4: End program\n");
scanf("%d", &mode);
}
else if (mode == 2)
{
printf("Please enter the size for triangle: ");
scanf("%d", &n);
// check whether the input number is within the range
while (n < 2 || n > 9)
{
printf("The size has to be between 2 and 9. Please enter again: ");
scanf("%d", &n);
}
// output the triangle using nested loop
for (int i = 1; i <= n; i++)
{
for (int j = n; j > 0; j--)
{
if (j <= i)
printf("%d ", j);
else
printf(" ");
}
for (int j = 2; j <= n; j++)
{
if (j <= i)
printf("%d ", j);
else
printf(" ");
}
printf("\n");
}
printf("Please choose which pattern you want to print: (Enter [1/2/3/4])\n");
printf("1: Symmetry pattern\n");
printf("2: Number triangle\n");
printf("3: Square pattern with stars\n");
printf("4: End program\n");
scanf("%d", &mode);
}
else if (mode == 3)
{
printf("Please enter the size for square: ");
scanf("%d", &n);
// check whether the input number is within the range
while (n < 2 || n > 9)
{
printf("The size has to be between 2 and 9. Please enter again: ");
scanf("%d", &n);
}
bool count = true;
bool check = true;
int output = 1;
// output the square using nested loop
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (check == true)
{
printf("%d ", output);
// if the output number is bigger than 9, than start to decrease
// if the output number is smaller than 1, than start to increase
if (count == true)
output++;
else
output--;
if (output > 8)
count = false;
if (output < 2)
count = true;
}
else
{
printf("* ");
}
// switch the number and *
check = !check;
if ((n % 2 == 0) && (j == n))
check = !check;
}
printf("\n");
}
printf("Please choose which pattern you want to print: (Enter [1/2/3/4])\n");
printf("1: Symmetry pattern\n");
printf("2: Number triangle\n");
printf("3: Square pattern with stars\n");
printf("4: End program\n");
scanf("%d", &mode);
}
else
{
// check whether the input number is within the range
printf("The input mode should be 1 to 4. Please enter again: ");
scanf("%d", &mode);
}
}
printf("End Program");
}
```