C Primer Plus--第6章 C控制语句:循环

6.16编程练习

/*习题1:编写一个程序,创建一个26个元素的数组,并在其中存储26个小写字母。并让该程序显示该数组的内容。*/
#include<stdio.h>
#define SIZE 26

int main(void)
{
    int i;
    char score[SIZE];

    for(i = 0;i < SIZE;i++)
    score[i]='a'+i;

    for(i = 0;i < SIZE;i++)
    printf("%c",score[i]);
 
    printf("\n");
    return 0;
}
abcdefghijklmnopqrstuvwxyz
--------------------------------
Process exited after 0.2245 seconds with return value 0
请按任意键继续. . .
/*习题2:嵌套循环*/
#include<stdio.h>
#define ROW 5

int main(void)
{
    int i,j;
    for(i = 0;j<ROW;i++)
{
    for(j = 0; j <= i;j++)

    printf("$");
    printf("\n");
}
    return 0;
}
$
$$
$$$
$$$$
$$$$$
--------------------------------
Process exited after 0.237 seconds with return value 0
请按任意键继续. . .
/*习题3:嵌套循环*/
#include<stdio.h>
#define ROW 6

int main(void)
{
    int i,j;
    char ch;
    for(i = 0;i < ROW;i++)
    {
    for(j = 0;j <= i;j++)
 
    printf("%c",'F'-j);
    printf("\n");
    }
    return 0;
}
F
FE
FED
FEDC
FEDCB
FEDCBA
--------------------------------
Process exited after 0.5425 seconds with return value 0
请按任意键继续. . .
/*习题4:让程序要求用户输入一个大写字母,使用嵌套循环产生像下面这样的金字塔图案*/
#include<stdio.h>

int main(void)
{
	char ch;
	int num,e,f,g,h;
	
	printf("Please enter a char:\n");
	scanf("%c",&ch);
	
	num = ch - 'A';
	for(e = 0;e < num + 1;e++)
	{
		for(f = e ;f < num;f++)
		printf(" ");
		
		for(g = 0;g < e+1;g++)
		printf("%c",'A'+g);
		
		for(h = e;h > 0;h--)
		printf("%c",'A'+(h-1));
		
		printf("\n");
		
	}
	return 0;
}
Please enter a char:
E
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
--------------------------------
Process exited after 2.159 seconds with return value 0
请按任意键继续. . .
/*习题5:编写一个程序打印一个表,表的每一行都给出一个整数、它的平方以及它的立方。要求用户输入表的上限与下限。使用一个for循环。*/
#include<stdio.h>

int main(void)
{
	int i,min,max;
	printf("Please enter a min:\n");
	scanf("%d",&min);
	printf("Please enter a max:\n");
	scanf("%d",&max);
	
	printf("%5s %10s %15s\n","num","square","cube");
	for(i=min;i <= max;i++)
	{
		printf("%5d %10d %15d\n",i,i*i,i*i*i);
	}
	return 0;
}
Please enter a min:
1
Please enter a max:
10
  num     square            cube
    1          1               1
    2          4               8
    3          9              27
    4         16              64
    5         25             125
    6         36             216
    7         49             343
    8         64             512
    9         81             729
   10        100            1000
--------------------------------
Process exited after 2.325 seconds with return value 0
请按任意键继续. . .
/*习题6:编写一个程序把一个单词读入一个字符数组,然后反向打印出这个词。提示:使用strlen()(第4章)计算数组中最后一个字符的索引。*/
#include<stdio.h>
#include<string.h>

int main(void)
{
	 char str[40];
	 int num,i;
	 printf("Please enter a word!\n");
	 scanf("%s",str);
	 num = strlen(str);
	 for(i = num - 1;i >=0;i--)
	 
	 	printf("%c",str[i]);
	 	printf("\n");
	 
	return 0;
}
Please enter a word!
hello
olleh
--------------------------------
Process exited after 3.054 seconds with return value 0
请按任意键继续. . .
/*习题7:编写一个程序,要求输入两个浮点数,然后打印出用二者的差值除以二者的乘积所得的结果。在用户键入非数字的输入之前程序循环处理每对输入值。*/
#include<stdio.h>

int main(void)
{
	float a,b,result;
	
	printf("Please enter a pair of number:\n");
	while(scanf("%f %f",&a,&b) == 2)
	{
		result = (a-b) / (a*b);
		printf("(%.3f-%.3f)/(%.3f*%.3f)=%.5f\n",a,b,a,b,result);
		printf("Please enter the next pair of number:\n");
	}
	return 0;
 } 
Please enter a pair of number:
9.8
3.3
(9.800-3.300)/(9.800*3.300)=0.20099
Please enter the next pair of number:
q
--------------------------------
Process exited after 63.62 seconds with return value 0
请按任意键继续. . .
/*习题8:对练习7进行修改,让它使用一个函数来返回计算值。*/
#include<stdio.h>
float cal(float a,float b);

int main(void)
{
	float a,b,result;
	
	printf("Please enter a pair of number:\n");
	while(scanf("%f %f",&a,&b) == 2)
	{
	result = cal(a,b);
	printf("(%.3f-%.3f)/(%.3f*%.3f)=%.5f\n",a,b,a,b,result);
    printf("Please enter the next pair of number:\n");
	}
	return 0;
 } 
 	float cal(float a,float b)
 	{
 		return (a-b) / (a*b);
	 }
Please enter a pair of number:
9.8
3.3
(9.800-3.300)/(9.800*3.300)=0.20099
Please enter the next pair of number:
/*习题9:编写一个程序,要求用户输入下限整数和一个上限整数,然后,依次计算从下限到上限的 
每一个整数的平方的加和,最后显示结果。程序将不断提示用户输入下限整数和上限整数 
并显示出答案,直到用户输入的上限整数等于或小于下限整数为止。程序运行的结果应该 
如下所示: 
Enter lower and upper integer limits: 5 9 
The sums of the squares from 25 to 81 is 255 
Enter next set of limits: 3 25 
The sums of the squares from 9 to 625 is 5520 
Enter next set of limits: 5 5 
Done */
#include<stdio.h>

int main(void)
{
	int min,max;
	int i,c;
	
	printf("Enter lower and upper integer limits:");
    while((scanf("%d %d",&min,&max)),max>min)
	{
        for(i = min,c = 0;i <= max;i++)
        {
        	c += i * i;
		}
	    printf("The sums of the squares from %d to %d is %d\n",min*min,max*max,c);
		printf("Enter next set of limits:");

	}
	printf("Done!\n");
	return 0;
}
Enter lower and upper integer limits:5 9
The sums of the squares from 25 to 81 is 255
Enter next set of limits:
/*习题10:编写一个程序把8个整数读入一个数组中,然后以相反的顺序打印它们。*/
#include<stdio.h>
#define SIZE 8

int main(void)
{
	int num[SIZE];
	int i;
	
	printf("Please enter 8 numbers:\n");
	
	for(i = 0;i < 8;i++)
	scanf("%d",&num[i]);
	for(i = SIZE-1;i >= 0;i--)
	printf("%d",num[i]);
	
	printf("\n");
	return 0;
 } 
Please enter 8 numbers:
1
2
3
4
5
6
7
8
87654321
--------------------------------
Process exited after 5.359 seconds with return value 0
请按任意键继续. . .
/*习题11:考虑这两个无限序列:

    1.0+1.0/2.0+1.0/3.0+1.0/4.0+…

    1.0 - 1.0/2.0+1.0/3.0 - 1.0/4.0+…

编写一个程序来计算这两个序列不断变化的总和,直到达到某个次数。让用户交互地输入这个次数。

看看在20次、100次和500次之后的总和。是否每个序列都看上去要收敛于某个值?提示:奇数个-1相乘的值为-1,而偶数个-1相乘的值为1。*/
#include<stdio.h>

int main(void)
{
	int count;
	double sum1 = 0,sum2 = 0;
	double i;
	int sign = 1;
	
	printf("Please enter the count:");
	scanf("%d",&count);
	
	for(i = 1.0;i <= count;i++,sign = 0-sign)
	{
		sum1 += 1.0/i;
		sum2 += sign*1.0/i;
	}
	printf("sum1 = %lf\n",sum1);
	printf("sum2 = %lf\n",sum2);

	return 0;
}
Please enter the count:20
sum1 = 3.597740
sum2 = 0.668771
--------------------------------
Process exited after 1.622 seconds with return value 0
请按任意键继续. . .
/*习题12:编写一个程序,创建一个8个元素的int数组,并且把元素分别设置为2的前8次幂,然后打印出它们的值。使用for循环来设置值;为了变化,使用do while循环来显示这些值。*/
#include<stdio.h>
#include<math.h>

int main(void)
{
	int num[8];
	int 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;
 } 
1       2       4       8       16      32      64      128
--------------------------------
Process exited after 0.8399 seconds with return value 0
请按任意键继续. . .
/*习题13:编写一个程序,创建两个8元素的double数组,使用一个循环来让用户键入第一个数组的8个元素的值。
程序把第二个数组的元素设置为第一个数组元素的累积和。例如,第二个数组的第4个元素应该等于第一个数组的前
4个元素的和,第二个数组的第5个元素应该等于第一个数组的前5个元素的和(使用嵌套循环可以做到这一点。不过
利用第二个数组的第5个元素等于第二个数组的第4个元素加上第一个数组的第5个元素这一事实,可以避免嵌套而
只使用单个循环来完成这个任务)。最后,使用一个循环来显示两个数组中的内容,第一个数组在一行中显示,而第
二个数组中的每个元素在第一个数组的对应元素之下进行显示。*/
#include<stdio.h>
#define SIZE 8

int main(void)
{
    double one [SIZE];
    double two [SIZE];
    int i;
    
    printf("Please enter the number:\n");
    
    for(i = 0;i < 8;i++)
    scanf("%lf",&one[i]);

    two[0] = one[0];
    
    for(i = 0;i < 8;i++)
    two[i] = two[i-1] + one[i];
    
    for(i = 0;i < 8;i++)
    printf("%5.2lf",one[i]);
    printf("\n");

    for(i = 0;i < 8;i++)
    printf("%5.2f",two[i]);
    printf("\n");
    
  return 0;
}
Please enter the number:
1.1
2.2
3.3
4.4
5.5
6.6
7.7
8.8
    1.10    2.20    3.30    4.40    5.50    6.60    7.70    8.80
    1.10    3.30    6.60   11.00   16.50   23.10   30.80   39.60

--------------------------------
Process exited after 11.9 seconds with return value 0
请按任意键继续. . .
/*习题14:编写一个程序读入一行输入,然后反向打印该行。您可以把输入存储在一个char数组中;假定该行不超过255个字符。
回忆一下,您可以使用具有%c说明符的scanf()从输入中一次读入一个字符,而且当您按下回车键时会产生换行字符(\n)。*/
#include<stdio.h>
#define LEN 10

int main(void)
{
	char ch[255];
	int i;
	
	printf("Please enter the number:\n");
	
	for(i = 0;i < LEN;i++)
	scanf("%c",&ch[i]);
	
	for(i = LEN - 1;i >= 0;i--)
	printf("%c",ch[i]);
	
	printf("\n");

	return 0; 
}
Please enter the number:
qwertyuiop
poiuytrewq

--------------------------------
Process exited after 4.847 seconds with return value 0
请按任意键继续. . .
/*习题15:Daphne以10%的单利息投资了100美元(也就是说,每年投资赢得的利息等于原始投资的10%)Deirdre则以每年
5%的复合利息投资了10O美元(也就是说,利息是当前结余的5%,其中包括以前的利息)。编写一个程序,计算需要多少年
Deirdre的投资才会超过Daphne,并且显示出到那时两个人的投资额。*/
#include<stdio.h>

int main(void)
{
    double Daphne = 100,Deirdre = 100;
    int i = 0;
    
    while(Daphne >= Deirdre)
    {
    Daphne += 100 * 0.1;
    Deirdre += Deirdre * 0.05;
    i++;
 }
    printf("After %d years,Daphne has less than Deirdre.\n",i);
	printf("Daphne = %lf,Deirdre = %lf\n",Daphne,Deirdre);
    
    return 0;
}
After 27 years,Daphne has less than Deirdre.
Daphne = 370.000000,Deirdre = 373.345632

--------------------------------
Process exited after 0.317 seconds with return value 0
请按任意键继续. . .
/*习题16:Chuckie Lucky赢了100万美元,他把它存入一个每年赢得8%的帐户。在每年的最后一天,Chuckie取出10万美元。
编写一个程序,计算需要多少年Chuckie就会清空他的帐户。*/
#include<stdio.h>

int main(void)
{
  double money = 1000000;
  int years;
  
  while(money > 0)
  {
    money += money *0.08;
    money -= 100000;
    years++;
   }
   
printf("After %d year,Chuckie has no money!",year);

return 0;
}
After 21 year,Chuckie has no money!
--------------------------------
Process exited after 0.2358 seconds with return value 0
请按任意键继续. . .
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值