C语言学习《C Primer Plus》编程练习第六章

1

#include  
int main()
{
	char a[26];
	char ch;
	int i;
	for(i=0;i<26;i++)
	{
		a[i]='a'+i;
		printf("%c ",a[i]);
	}
	return 0;
}

2

#include 
int main()
{
	int i,j;
	for(i=0;i<5;i++)
	{
		for(j=0;j<=i;j++)
		{
			if(j==i)
				printf("$\n");	
			else
				printf("$"); 
		}
	}
}

3

#include 
int main()
{
	int i,j;
	char ch;
	for(i=0;i<6;i++)
	{
		for(j=0;j<=i;j++)
		{
			if(j==i)
				printf("%c \n",'F'-i);
			else
				printf("%c",'F'-j);
		}
	}
	return 0;
}

4

#include 
int main()
{
	int i,j,n=0;
	char ch;
	for(i=0;i<6;i++)
	{
		for(j=0;j<=i;j++)
		{
			printf("%c",'A'+n);
			if(j==i)
				printf("\n");
			n++;
		}
	}
}

 

5

# include 
int main()
{
	int i,j,k,n;
	char ch;
	char a[]={'A','B','C','D','E','F','G','H','I','G','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
	printf("Please enter a letter(大写):\n");
	scanf("%c",&ch);
	for(k=0;k<26;k++)
	{
		if(ch==a[k])
		{
			n=k+1;
		}
	}

	for(i=0;i=0;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");
	}
	
	
}

 

6

#include 
int power(int i);
int cubic(int i);
int main()
{
	int min,max,i;
	printf("Please enter the less number: ");
	scanf("%d",&min);
	printf("Please enter the lagest number: ");
	scanf("%d",&max);
	printf("Number          power           cubic\n");
	for(i=min;i<=max;i++)
	{
		printf("%d		%d		%d\n",i,power(i),cubic(i));
	}
} 
int power(int i)
{
	i=i*i;
	return i;
}
int cubic(int i)
{
	i=i*i*i;
	return i;
}

 

7

#include 
#include 
int main()
{
	char a[100];
	int i,n;
	printf("Please enter a word: ");
	scanf("%s",&a);
	n=strlen(a);
	printf("The string is long:%d\n",n); 
	for(i=n-1;i>=0;i--)
	{
		printf("%c",a[i]);
	}
	
}

 

8

#include 
int main()
{
	float number1,number2;
	int a,b;
	while(1)
	{
		number1=number2=0;
		printf("Please enter numberone :");
		a=scanf("%f",&number1);
		if(a==0)                   //如果是纯数字,scanf返回值为1,否则为0 
		{
			printf("Wrong");
			break;
		}
		printf("Please enter numbertwo :");
		b=scanf("%f",&number2);	
		if(b==0)
		{
			printf("Wrong");
			break;
		}
		printf("The result of (numberone-numbertwo/(numberone*numbertwo):");
		printf("%f\n",(number1-number2)/(number1*number2));	
		
	}
}

9

#include 
void result(float number1,float number2); 
int main()
{
	float number1,number2;
	int a,b;
	while(1)
	{
		number1=number2=0;
		printf("Please enter numberone :");
		a=scanf("%f",&number1);
		if(a==0)                   //如果是纯数字,scanf返回值为1,否则为0 
		{
			printf("Wrong");
			break;
		}
		printf("Please enter numbertwo :");
		b=scanf("%f",&number2);	
		if(b==0)
		{
			printf("Wrong");
			break;
		}
		result(number1,number2); 	
	}
}
void result(float number1,float number2)
{
		printf("The result of (numberone-numbertwo/(numberone*numbertwo):");
		printf("%f\n",(number1-number2)/(number1*number2));	
}

10

#include 
int main()
{
	int lower,upper,i,sum=0;
	printf("Enter lower and upper integer limits:");
	scanf("%d",&lower);
	scanf("%d",&upper);
	for(i=lower;i<=upper;i++)
		sum+=i*i;
	printf("The sums of the squares from %d to %d is %d\n",lower*lower,upper*upper,sum);
	while(1)
	{
		sum=0;
		printf("Enter next set of limits:");
		scanf("%d",&lower);
		scanf("%d",&upper);
		if(lower

11

#include 
int main()
{
	int num[10],i;
	printf("Please enter ten numbers:");
	for(i=0;i<10;i++)
	{
		scanf("%d",&num[i]);
	}
	printf("The numbers are:");
	for(i=0;i<10;i++)
	{
		printf("%d ",num[i]);
	}
	printf("\n");
	printf("The new sort of numbers:");
	for(i=9;i>=0;i--)
	{
		printf("%d ",num[i]);
	}
}

12

#include 
int main()
{
	int n,i;
	double sumone=0,sumtwo=0;
	while(1)
	{
		printf("请输入你想运算的次数:");
		scanf("%d",&n); 
		if(n<=0)
			{
				printf("Wrong");
				break;
			}
		else
		{
			for(i=1;i<=n;i=i+1)
			{
				sumone+=1.0/i;
				if(i%2==0)
					sumtwo-=1.0/i;
				else
					sumtwo+=1.0/i;
			}
			printf("1.0+1.0/2.0+1.0/3.0+…:%lf\n",sumone);
			printf("1.0-1.0/2.0+1.0/3.0+…: %lf\n",sumtwo);
		}
}
}

13

#include 
int main()
{
	int i,n=1;
	int a[8];
	
	for(i=0;i<8;i++)
	{
		n=n*2;
		a[i]=n;
	}
	printf("\n");
	printf("结果是:");
	i=0;
	do{
		printf("%d ",a[i]);
		i++;
	}while(i>=0&&i<8); 
}

14

#include 
int main()
{
	double  rowo[8],rowt[8],sum;
	int i,j;
	for(i=0;i<8;i++)
	{
		printf("请输入第%d个数:",i+1);
		scanf("%lf",&rowo[i]);
	}
	for(i=0;i<8;i++)
	{
		sum=0;
		for(j=0;j<=i;j++)
		{
			sum+=rowo[j]; 
		}
		rowt[i]=sum;
	}
	printf("输入的数值是:");
	for(i=0;i<8;i++)
	{
		printf("%lf ",rowo[i]);
	}
	printf("\n");
	printf("输出的数值是:");
	for(i=0;i<8;i++)
	{
		printf("%lf ",rowt[i]);
	}
	printf("\n");
}

 

15

#include 
#include 
int main()
{
	char a[255];
	int n,i;
	printf("请输入一个字符串:");
	scanf("%s",&a);
	n=strlen(a);
	printf("倒叙输出为:");
	for(i=n-1;i>=0;i--)
	{
		printf("%c",a[i]);
	}
}

16

#include 
int main()
{
	double Dap = 100.0;
    double Dei = 100.0;
    int i = 0;
    do
    {
        i++;
        Dap = Dap + 10;
        Dei = Dei * 1.05;
    } while (Dei < Dap);
    printf("After %d year, Deirdre's account:%lf, Daphne's account:%lf\n", i, Dei, Dap);
}

17

#include 
int main()
{
	double Chuckie = 100;
    int i = 0;
    do
    {
        i++;
        Chuckie = Chuckie * 1.08;
        Chuckie -= 10;
    } while (Chuckie > 0);

    printf("After %d year, Chuckie has taken all of the money\n", i);

} 

18

#include 
int main()
{
	int i=0,n=5;
	do{
		i++;
		n=(n-i)*2;
		printf("Rabnud博士第%d周的朋友数为%d.\n",i,n);
	} while(n<=150);
}

 

你可能感兴趣的:(C语言)