F
FE
FED
FEDC
FEDCB
FEDCBA
--------------------------------
Process exited after 0.5425 seconds with return value 0
请按任意键继续...
/*习题4:让程序要求用户输入一个大写字母,使用嵌套循环产生像下面这样的金字塔图案*/#include<stdio.h>intmain(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");}return0;}
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>intmain(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);}return0;}
Please enter a min:1
Please enter a max:10
num square cube
111248392741664525125636216749343864512981729101001000--------------------------------
Process exited after 2.325 seconds with return value 0
请按任意键继续...
/*习题6:编写一个程序把一个单词读入一个字符数组,然后反向打印出这个词。提示:使用strlen()(第4章)计算数组中最后一个字符的索引。*/#include<stdio.h>#include<string.h>intmain(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");return0;}
Please enter a word!
hello
olleh
--------------------------------
Process exited after 3.054 seconds with return value 0
请按任意键继续...
/*习题7:编写一个程序,要求输入两个浮点数,然后打印出用二者的差值除以二者的乘积所得的结果。在用户键入非数字的输入之前程序循环处理每对输入值。*/#include<stdio.h>intmain(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");}return0;}
Please enter a pair of number:9.83.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>floatcal(float a,float b);intmain(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");}return0;}floatcal(float a,float b){return(a-b)/(a*b);}
Please enter a pair of number:9.83.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>intmain(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");return0;}
Enter lower and upper integer limits:59
The sums of the squares from 25 to 81 is 255
Enter next set of limits:
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>intmain(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");return0;}
1248163264128--------------------------------
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 8intmain(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");return0;}
Please enter the number:1.12.23.34.45.56.67.78.81.102.203.304.405.506.607.708.801.103.306.6011.0016.5023.1030.8039.60--------------------------------
Process exited after 11.9 seconds with return value 0
请按任意键继续...
/*习题14:编写一个程序读入一行输入,然后反向打印该行。您可以把输入存储在一个char数组中;假定该行不超过255个字符。
回忆一下,您可以使用具有%c说明符的scanf()从输入中一次读入一个字符,而且当您按下回车键时会产生换行字符(\n)。*/#include<stdio.h>#define LEN 10intmain(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");return0;}
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>intmain(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);return0;}
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>intmain(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);return0;}
After 21 year,Chuckie has no money!--------------------------------
Process exited after 0.2358 seconds with return value 0
请按任意键继续...