6.16 编程练习
1.
#include<stdio.h>
#define L 26
int main(void)
{
char letter[L];
for(int i=0;i<L;i++)
letter[i]='a'+i;
for(int i=0;i<L;i++)
printf("%c",letter[i]);
return 0;
}
2.
#include<stdio.h>
int main(void)
{
int i,j;
for(i=0;i<5;i++)
{ for(j=0;j<=i;j++)
printf("$");
printf("\n");
}
return 0;
}
3.
#include<stdio.h>
int main(void)
{
int i,j;
for(i=0;i<6;i++)
{
char letter='F';
for(j=0;j<=i;j++)
printf("%c",letter--);
printf("\n");
}
return 0;
}
*4.
让程序要求用户输入一个大写字母,使用嵌套循环产生像下面这样的金字塔图案:
A
ABA
ABCBA
ABCDCDA
ABCDEDCBA
#include<stdio.h>
int main(void)
{
#include<stdio.h>
int main(void)
{
int i, j, k;
char a;
scanf("%c", &a);
k = a - 'A' + 1;
for (i = 0; i<k; i++)
{
for (j = 0; j<k - i; j++)
printf(" ");
for (j = 0; j<=i; j++)
printf("%c", 'A' + j);
for (j = i - 1; j>=0; j--)
printf("%c", 'A' + j);
printf("\n");
}
return 0;
}
5.
#include<stdio.h>
int main(void)
{
int i, min, max;
printf("Please input the min and max:");
scanf("%d %d", &min, &max);
printf("%10s%10s%10s\n", "num", "square", "cube");
for (i = min; i <= max; i++)
{
printf("%10d%10d%10d\n", i, i*i, i*i*i);
}
return 0;
}
6.
#include<stdio.h>
#include<string.h>
int main(void)
{
char word[20];
int i;
printf("Please input a word:");
scanf("%s", &word);
for (i = strlen(word) - 1; i >= 0; i--)
printf("%c", word[i]);
printf("\n");
return 0;
}
7.
#include<stdio.h>
int main(void)
{
float a, b;
printf("Please input two floats:");
while (scanf("%f%f", &a, &b) == 2)
{
printf("(%.2f-%.2f) / (%.2f*%.2f) = %.2f\n", a, b, a, b, (a - b) / (a*b));
printf("Please input two floats:");
}
printf("end\n");
return 0;
}
8.
#include<stdio.h>
float calculate(float, float);
int main(void)
{
float a, b;
printf("Please input two floats:");
while (scanf("%f%f", &a, &b) == 2)
{
printf("(%.2f-%.2f) / (%.2f*%.2f) = %.2f\n", a, b, a, b, calculate(a, b));
printf("Please input two floats:");
}
printf("end\n");
return 0;
}
float calculate(float x, float y)
{
return((x - y) / (x*y));
}
9.
#include<stdio.h>
int main(void)
{
int lower, upper, sum, i;
printf("Enter lower and upper integer limits:");
scanf("%d%d", &lower, &upper);
while (lower<upper)
{
for (i = lower, sum = 0; i <= upper; i++) sum += i*i;
printf("The sums of the squates from %d to %d is %d\n", lower*lower, upper*upper, sum);
printf("Enter next set of limits:");
scanf("%d%d", &lower, &upper);
}
printf("Done\n");
return 0;
}
10.
#include<stdio.h>
int main(void)
{
int num[8], i;
printf("Enter 8 numbers:");
for (i = 0; i<8; i++)
scanf("%d", &num[i]);
for (i = 7; i >= 0; i--)
printf("%d\t", num[i]);
printf("\n");
return 0;
}
11.
#include<stdio.h>
int main(void)
{
double i, sum1, sum2;
int sign, count;
printf("Enter the count:");
scanf("%d", &count);
for (i = 1.0, sign = 1, sum1 = 0, sum2 = 0; i <= count; i++, sign -= sign)
{
sum1 += 1.0 / i;
sum2 += sign * 1.0 / i;
}
printf("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 ...(%d terms) = %lf\n", count, sum1);
printf("1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 ...(%d terms) = %lf\n", count, sum2);
return 0;
}
*12.
#include<stdio.h>
#include<math.h>
int main(void)
{
int num[8], i;
for (i = 0; i<8; i++)
num[i] = pow(2., i);
i = 0;
do
printf("%d\t", num[i]);
while (++i<8);
printf("\n");
return 0;
}