C primer plus第6章(c控制语句:循环)习题

来源:互联网 发布:sql语句怎么注释 编辑:程序博客网 时间:2024/05/16 06:51

1

#include <stdio.h>int main (void){    char letters[26];    int i;    for(i=0;i<26;i++) {        letters[i] = (char)('a'+i);    }    for(i=0;i<26;i++) {        printf("%c", letters[i]);    }}

2

#include <stdio.h>int main (void){    int i,j,rows;    rows=5;    for(i=0; i<rows; i++)    {        for(j=0; j<=i; j++)        {            printf("$");        }        printf("\n");    }}

3


#include <stdio.h>int main (void){    int i, j;    char top, bottom;    printf("请输入塔顶字母:\n");    scanf("%c", &top);    //清除缓冲器的残留值(回车)    fflush(stdin);    printf("请输入塔底字母:\n");    scanf("%c", &bottom);    for(i = top; i <= bottom; i++)    {        //控制空格        for(j = bottom - i; j > 0; j--)        {            printf(" ");        }        //控制左边字符        for(j = 0; j <= i - top; j++)        {            printf("%c", top + j);        }        //控制右边字符        for(j -= 2; j >= 0; j--)        {            printf("%c", top + j);        }        printf("\n");    }    return 0;}

5

#include <stdio.h>int main (void){    int i,top, bottom;    printf("请输入下限数字:\n");    scanf("%d", &top);    //清除缓冲器的残留值(回车)    fflush(stdin);    printf("请输入上限数字:\n");    scanf("%d", &bottom);    for(i = top; i <= bottom; i++)    {        printf("%d    %d    %d\n", i, i * i, i * i * i);    }    return 0;}

6.

#include <stdio.h>int main (void){    int i, n;    char c[50];    printf("请输入字符串:\n");    scanf("%s", c);    n = strlen(c);    for(i = n-1; i >= 0; i--)    {        printf("%c", c[i]);    }    return 0;}

7.

#include <stdio.h>int main (void){    double d1,d2,res;    printf("请输入2个数字以逗号分隔");    while(scanf("%lf,%lf", &d1, &d2) == 2) {        res = (d1 - d2) / (d1 * d2);        printf("(%lf - %lf) / (%lf * %lf) = %lf\n", d1, d2, d1, d2, res);    }    return 0;}

8.

#include <stdio.h>double resFun(double d1, double d2) {    return (double)(d1 - d2) / (d1 * d2);}int main (void){    double d1,d2,res;    printf("请输入2个数字以逗号分隔\n");    while(scanf("%lf,%lf", &d1, &d2) == 2) {        res = resFun(d1, d2);        printf("(%lf - %lf) / (%lf * %lf) = %lf\n", d1, d2, d1, d2, res);    }    return 0;}

9.

#include <stdio.h>//传引用_Bool scanfFun(int *minInt, int *maxInt){    printf("请输入下限整数\n");    if(scanf("%d", &*minInt) != 1)    {        return 0;    }    fflush(stdin);    printf("请输入上限整数\n");    if(scanf("%d", &*maxInt) != 1)    {        return 0;    }    return 1;}int main (void){    int minInt, maxInt, sum, i;    sum = 0;    //传引用    while(scanfFun(&minInt, &maxInt))    {        for(i = minInt; i <= maxInt; i++)        {            sum += i * i;        }        printf("%d\n", sum);    }    return 0;}


10

#include <stdio.h>#define SIZE 8int main (void){    int intArr[SIZE];    int i;    printf("请依次输入8个整数\n");    scanf("%d,%d,%d,%d,%d,%d,%d,%d", &intArr[0], &intArr[1], &intArr[2], &intArr[3], &intArr[4], &intArr[5], &intArr[6], &intArr[7]);    for(i = SIZE - 1; i >= 0; i--)    {        printf("%d,", intArr[i]);    }    return 0;}

11

#include <stdio.h>#define STEP 1.0int main (void){    int i, times;    double sum, diff, temp;    sum = diff = 0.0;    printf("请输入次数\n");    while(scanf("%d", ×) == 1)    {        for(i = 1; i <= times; i++)        {            temp = STEP / (float)i;            sum += temp;            diff -= temp;        }        printf("sum = %lf, diff = %lf\n", sum, diff);    }    return 0;}

12

#include <stdio.h>#define SIZE 8#define STEP 2int main (void){    int val[SIZE], temp, i;    temp = STEP;    for(i = 0; i < SIZE; i++)    {        temp *= STEP;        val[i] = temp;    }    i = 0;    do    {        printf("%d\n", val[i]);    }    while(i++ < 7);    return 0;}

13

#include <stdio.h>#define SIZE 8#define STEP 2int main (void){    double valA[SIZE], valB[SIZE], temp;    int i;    temp = 0.0;    printf("请依次输入%d个浮点型\n", SIZE);    for(i = 0; i < SIZE; i++)    {        scanf("%lf", &valA[i]);    }    for(i = 0; i < SIZE; i++)    {        temp += valA[i];        valB[i] = temp;        printf("%lf\n", valB[i]);    }    return 0;}

14

#include <stdio.h>#define SIZE 10int main (void){    char val[SIZE];    int i;    printf("请依次输入%d个字符\n", SIZE);    for(i = 0; i < SIZE; i++)    {        scanf("%c", &val[i]);        fflush(stdin);    }    for(i = SIZE - 1; i >= 0; i--)    {        printf("%c", val[i]);    }    return 0;}

15

#include <stdio.h>#define STEP 100int main (void){    double daphne, deirdre;    int years = 0;    daphne = deirdre = STEP;    while(daphne >= deirdre) {        daphne += STEP * 0.1;        deirdre += deirdre * 0.05;        years++;    }    printf("%d", years);    return 0;}

16

#include <stdio.h>#define TOTAL 1000000.00#define RATE 0.08#define STEP 100000.00int main (void){    double remain = TOTAL;    int years = 0;    while(remain >= 0) {        remain += remain * RATE;        remain -= STEP;        years++;    }    printf("%d", years);    return 0;}


原创粉丝点击