自学C第6天

一系列C语言编程练习题,包括打印字母表、数字序列、倒序输出字符串、计算平方根比例、字符输入与倒序输出、用户输入数值求和、读取两个数并计算它们的商、自定义函数计算比例、计算平方和立方的序列、以及数组元素的反向输出。这些练习涵盖了循环控制、输入输出、函数调用等基本编程概念。
摘要由CSDN通过智能技术生成

C Primer Plus 第6章

第3题:

#include <stdio.h>

int main(void)

{

char ch;

int row, b;

for (row = 1; row <= 6; row++)

{

for (ch = 'F', b = 1; b <= row; b++, ch--)

printf("%c", ch);

printf("\n");

}

return 0;

}

第4题:

#include <stdio.h>

int main(void)

{

char ch = 'A'; #A不需要每次都被初始化

int row, b;

for (row = 1; row <= 6; row++)

{

for ( b = 1; b <= row ; b++, ch++)

printf("%c", ch);

printf("\n");

}

return 0;

}

第5题: 这题太难了 没做出来

#include <stdio.h>

int main(void)

{

int x, y;

char ch;

printf("Please enter a letter you want to print: ");

scanf("%c", &ch);

int row = ch - 'A';

for (x = 0; x <= row; x++)

{

char t = 'A' - 1;

for (y = 0; y <= row - x; y++)

printf(" ");

for (y = 0; y <= x; y++)

printf("%c", ++t);

for (y = 0; y < x; y++)

printf("%c", --t);

printf("\n");

}

return 0;

}

第6题:

#include <stdio.h>

int main(void)

{

int low, up, a;

printf("Please enter the lowest limit of the number:");

scanf("%d", &low);

printf("Please enter the upest limit of the number: ");

scanf("%d", &up);

for (a = low; a <= up; a++)

{

printf("%10d %10d %10d\n", a, a * a, a * a * a);

}

printf("Done.");

return 0;

}

第7题:

#include <stdio.h>

#include <string.h>

#define LEN 20

int main(void)

{

int i;

char str[LEN];

printf("Please enter a word:");

scanf("%19s", str);

printf("The word is:\n");

printf("%s\n", str);

printf("Reversing the word is:\n");

for (i = strlen(str) - 1; i >= 0; i--)

{

printf("%c", str[i]);

}

return 0;

}

第8题

#include <stdio.h>

int main(void)

{

float a, b;

int status;

printf("Please enter the first number: ");

status = scanf("%f", &a);

printf("Please enter the second number: ");

scanf("%f", &b);

while (status == 1) #注:用这条语句更好(scanf("%lf %lf", &a, &b) == 2 上面部分就可以去掉,程序更加简单

{

printf("%f\n", (a - b) / (a * b));

printf("Please enter the first number: ");

status = scanf("%f", &a);

printf("Please enter the second number: ");

scanf("%f", &b);

}

return 0;

}

第9题:

#include <stdio.h>

float method(float n, float m); #注:定义函数时要声明函数的类型如 float,int ,char, ()中的参数也需要定义

int main(void)

{

float a, b, reslut;

int status;

printf("Please enter the first number: ");

status = scanf("%f", &a);

printf("Please enter the second number: ");

scanf("%f", &b);

while (status == 1)

{

reslut = method(a, b);

printf("%f\n", reslut);

printf("Please enter the first number: ");

status = scanf("%f", &a);

printf("Please enter the second number: ");

scanf("%f", &b);

}

return 0;

}

float method(float n, float m)

{

return (n - m) / (n * m);

}

第10题:

#include <stdio.h>

int main(void)

{

int lower, upper, a;

printf("Enter lower and upper integer limits: ");

scanf("%d %d", &lower, &upper);

int sum_s;

while (upper > lower) { #注:这里可以写成while ((scanf("%d %d", &low, &upp) == 2) && (upp > low))上方的scanf可以合并到这里

for (a = lower, sum_s = 0; a <= upper; a++) {

sum_s = sum_s + a * a; #注:可以写成sum_s += a*a

}

printf("The sums of the squares from %d to %d is %d\n",

lower*lower, upper*upper, sum_s);

printf("Enter next set of limits: ");

scanf("%d %d", &lower, &upper);

}

printf("done!");

return 0;

}

第11题:

#include <stdio.h>

#define N 8 #注:define前面要加#

int main(void)

{

int i, a[N]; #注:声明一个整数i 和一个8位的整数数组

printf("Please enter 8 numbers:\n");

for (i = 0; i < N; i++)

{

scanf("%d", &a[i]);

}

printf("Reverse order printing 8 numbers:\n");

for (i = N - 1; i >= 0; i--)

{

printf("%3d", a[i]);

}

return 0;

}

题目变得越来越难了,做题速度也越来越慢,有些题感觉过一边肯定不能一次搞懂

等看完书再过来多过几遍

加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值