• Chapter 6 : C Control Statements : Looping


    8. Given the input Go west, young man!, what would each of the following programs produce for output? (The ! follows the space character in the ASCII sequence.)

    a .

    #include <stdio.h>
    
    
    int main(void) {
        char ch;
    
        scanf("%c", &ch);
        while (ch != 'g') {
            printf("%c", ch);
            scanf("%c", &ch);
        }
        return 0;
    }
    

    Go west, youn!

    b. 

    #include <stdio.h>
    
    int main(void)
    {
        char ch;
    
        scanf("%c", &ch);
        while ( ch != 'g' )
        {
            printf("%c", ++ch);
            scanf("%c", &ch);
        }
        return 0;
    }
    

    Hp!xftu-!zpvo

    c. 

    #include <stdio.h>
    
    int main(void)
    {
        char ch;
        do {
            scanf("%c", &ch);
            printf("%c", ch);
    
        } while ( ch != 'g' ); 
        return 0;
    }
    

    Go west, young

    d.

    #include <stdio.h>
    
    int main(void) {
        char ch;
    
        scanf("%c", &ch);
        for (ch = '$'; ch != 'g'; scanf("%c", &ch))
            printf("%c", ch);
        return 0;
    }
    

    $o west, youn


    9. What will the following program print?

    #include <stdio.h>
    
    int main(void) {
        int n, m;
    
        n = 30;
        while (++n <= 33) printf("%d|", n);
    
        n = 30;
        do
            printf("%d|", n);
        while (++n <= 33);
    
        printf("
    ***
    ");
    
        for (
                n = 1;
                n * n <
                200; n += 4)
            printf("%d
    ", n);
    
        printf("
    ***
    ");
    
        for (
                n = 2, m = 6;
                n < m;
                n *= 2, m += 2)
            printf("%d %d
    ", n, m);
    
        printf("
    ***
    ");
    
        for (
                n = 5;
                n > 0; n--) {
            for (
                    m = 0;
                    m <=
                    n;
                    m++)
                printf("=");
            printf("
    ");
        }
        return 0;
    }
    

    31|32|33|30|31|32|33|

    *** 1

    5

    9

    13

     

    *** 2 6

    4 8

    8 10

     

    ***

    ======

    =====

    ====

    ===

    ==


    13. Define a function that takes an int argument and that returns, as a long, the square of that value.

    long square(int num) {
        return ((long) num) * num;
    }

    14. What will the following program print?

    #include <stdio.h>
    
    int main(void) {
        int k;
    
        for (k = 1, printf("%d: Hi!
    ", k); printf("k = %d
    ", k), k * k < 26; k += 2, printf("Now k is %d
    ", k))
            printf("k is %d in the loop
    ", k);
        return 0;
    }
    

    1: Hi!

    k = 1

    k is 1 in the loop Now k is 3

    k = 3

    k is 3 in the loop Now k is 5

    k = 5

    k is 5 in the loop Now k is 7

    k = 7

    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    反射
    接口和抽象类
    套接字通信
    C#的urlencode
    go语言摘记
    c# Httphelper帮助类 简约版
    maven
    读取资源文件里的值---来源practical-aspnetcore项目
    JWT使用---来源practical-aspnetcore项目
    .net core国际化
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829250.html
Copyright © 2020-2023  润新知