请教2个C语言编程的答案

来源:百度知道 编辑:UC知道 时间:2024/05/21 09:04:55
第一题
Write a C program to do the followings:
• Ask the user to print pattern 1 or 2 using switch
• Ask the user to input the size of the pattern
• Use for loops to generate the pattern.

Pattern 1 Pattern 2
***** *****
**** ****
*** ***
** **
* *
(Pattern1 是左对齐,Pattern2 是右对齐)
The program should follow the sample output.

Which pattern do you want to print (1,2)?1
Size of the pattern:4

****
***
**
*
(这个是左对齐)

Which pattern do you want to print (1,2)?2
Size of the pattern:4

****
***
**
*
(这个是右对齐)

Which pattern do you want to print (1,2)?6
Size of the pattern:4

Do not have this pattern.

第二题
Modify the previous program so that the program can print pattern 3 and 4. (You can also add other kind

基本的for语句练习题.
掌握for, switch, printf, scanf的用法应该不会觉得太难.
由于排版问题没法给你写程序. 自己看看书吧. 不难的.

//第一题
#include <stdio.h>
#include <string.h>

int main()
{
int n,p,i,j;
do{
printf("Which pattern do you want to print (1,2)?");
scanf("%d",&p);
printf("Size of the pattern:");
scanf("%d",&n);
switch(p)
{
case 1:
for(i=n;i>0;i--)
{
for(j=0;j<i;j++)
printf("*");
printf("\n");
}
break;
case 2:
for(i=n;i>0;i--)
{
for(j=0;j<n-i;j++)
printf(" ");
for(j=0;j<i;j++)
printf("*");
printf("\n");
}
break;
default:
printf("Do not have this pattern.\n");
break;
}
}while(1);
return 0;
}