第六章
#第六章编程练习
第一题:
#include<stdio.h>
#include<stdlib.h>
int main(){
char alphabet[26];
int i;//定义i为数组下标,c从字符a开始递增
char c='a';
for(i=0;i<26;i++,c++){
alphabet[i]=c;
printf("%c",alphabet[i]);
}
system("pause");
return 0;
}
第二题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j;
for(i=1;i<=5;i++){//外层控制行数
for(j=0;j<i;j++){//j<i表示第N行打印N个字符
printf("$");
}
printf("\n");
}
system("pause");
return 0;
}
第三题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j;
char c;
for(i=1;i<=6;i++){
for(j=0,c='F';j<i;j++,c--){//c--表示每次打印时,更新c,实现递减
printf("%c",c);
}
printf("\n");
}
system("pause");
return 0;
}
第四题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j;
char c='A';
for(i=1;i<=6;i++){
for(j=0;j<i;j++,c++){
printf("%c",c);
}
printf("\n");
}
system("pause");
return 0;
}
第五题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,num;
char c;
printf("Enter the core char you want to print(A...Z): ");
scanf("%c",&c);
char ch='A';
num=c-'A'+1;//输入字符的ASCII码减去‘A’加1得到的就是十进制结果num
//num就是需要打印的从A 开始的字符数,也是打印的行数
for(i=1;i<=num;i++){//打印行数
for(j=0;j<num-i;j++)
printf(" ");//打印空格,空格数=总字符数-当前行正序应打印的字符数
for(ch='A';j<num;j++)
printf("%c",ch++);//打印正序字符,需ch递增,j已经在空格时循环;
//ch做过初始化,所以只打印剩余字符
for(j=1,ch-=2;j<i;j++,ch--)
printf("%c",ch);//打印逆序字符,ch递减
printf("\n");
}
system("pause");
return 0;
}
第六题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int start,end;
printf("Please enter the start number:");
scanf("%d",&start);
printf("Please enter the end number:");
scanf("%d",&end);
printf(" Ori: Square: Cubic:\n");
int i=start;
for(i=start;i<=end;i++){
printf("%6d,%10d,%10d",i,i*i,i*i*i);
printf("\n");
}
system("pause");
return 0;
}
第七题:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char word[30];
printf("Please enter the words:");
scanf("%s",&word);
printf("The word you enter is : %s\n",word);
printf("The reverse word you enter is : ");
printf("%d",strlen(word));
int i=strlen(word)-1;
for(i=strlen(word)-1;i>=0;i--){
printf("\n%c\n",word[i]);
}
system("pause");
return 0;
}
第八题:
#include<stdio.h>
#include<stdlib.h>
int main(){
float x,y;
printf("Please enter the two foloat data(seprate by blank):");
while(scanf("%f %f",&x,&y)==2){//只有scanf函数读取了两个浮点型数据才等于2
printf("The answer is %f\n",(x-y)/(x*y));
printf("Please enter the two foloat data(seprate by blank):");
}
printf("Program end!");
system("pause");
return 0;
}
第九题:
#include<stdio.h>
#include<stdlib.h>
float calc(float x,float y);
int main(){
float x,y;
printf("Please enter the two foloat data(seprate by blank):");
while(scanf("%f %f",&x,&y)==2){
printf("The answer is %f\n",calc(x,y));
printf("Please enter the two foloat data(seprate by blank):");
}
printf("Program end!");
system("pause");
return 0;
}
float calc(float x,float y)
{
float result;
result=(x-y)/(x*y);
return result;
}
第十题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int lower,upper;
printf("Enter lower and upper integer limits:");
scanf("%d %d",&lower,&upper);
while(upper>lower){
int sum=0;
int i=lower;
for(i=lower;i<=upper;i++){
sum=sum+i*i;
}
printf("The sum of the squares form %d to %d is %d\n",lower,upper,sum);
printf("Enter lower and upper integer limits:");
scanf("%d %d",&lower,&upper);
}
printf("Done!");
system("pause");
return 0;
}
第十一题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int data[8];
int i;
printf("Enter the 8 integer data (seperate by blank):");
for(i=0;i<8;i++){
scanf("%d",&data[i]);
}
printf("Ok,the reverse data is :");
for(i=7;i>=0;i--){
printf(" %d",data[i]);
}
printf("\nDone!\n");
system("pause");
return 0;
}
第十二题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int length;
double sum=0.0;
printf("Enter the limit length:");
scanf("%d",&length);
while(length>0){
int i;
sum=0.0;
for(i=1;i<=length;i++){
sum=sum+1.0/i;
}
printf("The sum for 1.0+...+1.0/%d.0 is %lf\n",length,sum);
sum=0.0;
for(i=1;i<=length;i++){
if(i%2==0)sum=sum-1.0/i;
else sum=sum+1.0/i;
}
printf("The sum for 1.0-...+1.0/%d.0 is %lf\n",length,sum);
sum=0.0;
for(i=1;i<=length;i++){
if(i%2!=0)sum=sum+2*1.0/i;
}
printf("Tne sum for 1.0+1.0+2.0/3.0+...+2.0/%d.0 is %lf\n",length,sum);
printf("Enter the limit length:");
scanf("%d",&length);
}
printf("\nDone!\n");
system("pause");
return 0;
}
第十三题:
#include<stdio.h>
#include<stdlib.h>
int main(){
int data[8];
int i;
data[0]=2;
for(i=1;i<8;i++){
data[i]=data[i-1]*2;
}
int i=0;
do{
printf("%d ",data[i++]);
}while(i<8);
printf("\nDone!\n");
system("pause");
return 0;
}
第十四题:
#include<stdio.h>
#include<stdlib.h>
int main(){
double first[8],second[8];
int i;
printf("Enter 8 data to the FIRST array:");
for(i=0;i<8;i++){
scanf("%lf",&first[i]);
}
for(i=0;i<8;i++){
double sum=0;
int j;
for(j=0;j<=i;j++){
sum=sum+first[j];
}
second[i]=sum;
}
printf("All the data of two array:\n");
printf("First Array: ");
for(i=0;i<8;i++){
printf("%12lf. ",first[i]);
}
printf("\nSecond Array: ");
for(i=0;i<8;i++){
printf("%12lf. ",second[i]);
}
printf("\nDone!\n");
system("pause");
return 0;
}
第十五题:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char data[256];
printf("Enter the char in a line : ");
int i=0;
do{
scanf("%c",&data[i]);
}while(data[i]!='\n' && ++i);
printf("The reverse char of the data : ");
for(i--;i>=0;i--){
printf("%c",data[i]);
}
printf("\nDone!\n");
system("pause");
return 0;
}
第十六题:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
float daphne,deirdre;
daphne=deirdre=100.0;
int year =0;
do{
daphne=daphne+100*0.1;
deirdre=deirdre+deirdre*0.05;
year++;
}while((deirdre-daphne)<0);
printf("%d years later.\nDaphne = %f.\nDeirdre = %f.\n",year,daphne,deirdre);
printf("\nDone!\n");
system("pause");
return 0;
}
第十七题:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
float chuckie=100;
int year=0;
do{
chuckie=chuckie+chuckie*0.08;
chuckie-=10;
year++;
printf("%f\n",chuckie);
}while(chuckie>9);
printf("%d years later,Chuckie's account %f \n",year,chuckie);
printf("%d years later,Chuckie's account is null \n",++year);
printf("\nDone!\n");
system("pause");
return 0;
}
第十八题:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int rabnud=5;
int weeks=1;
while(rabnud<150){
printf("At %d weeks, Rabnud has %4d friends \n",weeks,rabnud);
rabnud=2*(rabnud-weeks++);
};
printf("\nDone!\n");
system("pause");
return 0;
}