zoukankan      html  css  js  c++  java
  • 例题第2章

    2.1

    #include<stdio.h>
    int main(void)
    {
        printf("Hello World!
    ");
    
    
        return 0;
    }

    2.2

    #include<stdio.h>
    int main(void)
    {
        printf("Programming is fun.
    ");
        printf("And programming in C is even more fun!
    ");
    
        return 0;
    }

    2.3

    #include<stdio.h>
    int main(void)
    {
        int celsius,fahr;
    
        fahr=100;
        celsius=5*(fahr-32)/9;
    
        printf("fahr=%d,celsius=%d
    ",fahr,celsius);
    
        return 0;
    }

    2.4

    #include<stdio.h>
    int main(void)
    {
        double x,y;
    
        printf("Enter x(x>=0):
    ");
        scanf("%lf",&x);
        if(x<=15){
            y=4*x/3;
        }
        else{
            y=2.5*x-10.5;
        }
        printf("y=f(%f)=%.2f
    ",x,y);
    
        return 0;
    }

    2.5

    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int money,year;
        double rate,sum;
    
        printf("Enter money:");
        scanf("%d",&money);
        printf("Enter year:");
        scanf("%d",&year);
        printf("Enter rate:");
        scanf("%lf",&rate);
        sum=money * pow(1 + rate,year);
        printf("sum = %.2f
    ",sum);
    
        return 0;
    }

    2.6

    #include<stdio.h>
    int main(void)
    {
        int fahr,lower,upper;
        double celsius;
    
        printf("Enter lower:");
        scanf("%d",&lower);
        printf("Enter upper:");
        scanf("%d",&upper);
        printf("fahr celsius
    ");
    
        for(fahr=lower;fahr<=upper;fahr++){
            celsius=(5.0/9.0)*(fahr-32);
            printf("%d%6.1f
    ",fahr,celsius);
        }
    
        return 0;
    }

    2.7

    #include<stdio.h>
    int main(void)
    {
        int i,n,sum;
    
        printf("Enter n:");
        scanf("%d",&n);
        sum=0;
        for(i=1;i<=n;i++){
            sum=sum+i;
        }
    
        printf("Sum of numbers from 1 to %d is %d
    ",n,sum);
    
        return 0;
    }

    2.8

    #include<stdio.h>
    int main(void)
    {
        int denominator,flag,i,n;
        double item,sum;
    
        printf("Enter n:");
        scanf("%d",&n);
    
        flag=1;
        denominator=1;
        sum=0;
    
        for(i=1;i<=n;i++){
            item=flag*1.0/denominator;
            sum=sum+item;
            flag=-flag;
            denominator=denominator+2;
        }
        printf("sum=%f
    ",sum);
    
        return 0;
    }

    2.9

    #include<stdio.h>
    int main(void)
    {
        int i,n;
        double product;
    
        printf("Enter n:");
        scanf("%d",&n);
        product=1;
        for(i=1;i<=n;i++){
            product=product*i;
        }
        printf("product=%.0f
    ",product);
    
        return 0;
    }

    2.10

    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int i,n;
        double power;
    
        printf("Enter n:");
        scanf("%d",&n);
        for(i=0;i<=n;i++){
            power=pow(2,i);
            printf("pow(2,%d)=%.0f
    ",i,power);
        }
    
        return 0;
    }

    2.11

    #include<stdio.h>
    double fact(int n);
    
    int main(void)
    {
        int i,n;
        double result;
    
        printf("Enter n:");
        scanf("%d",&n);
        for(i=0;i<=n;i++){
            result=fact(i);
            printf("%d!=%.0f
    ",i,result);
        }
    
        return 0;
    }
    
    double fact(int n)
    {
        int i;
        double product;
    
        product=1;
        for(i=1;i<=n;i++){
            product=product*i;
        }
    
        return product;
    }

  • 相关阅读:
    模态框 显示出模态框后在加载(可用模块框中加入editormd编辑器)
    python实现 列表内元素按照出现次数排序
    Selenium
    Python的Tqdm模块——进度条配置
    [Python3]selenium爬取淘宝商品信息
    如何用Matplotlib画一张好看的图
    maven如何引入本地jar
    tensorflow和bazel版本对应问题及对应的bazel安装
    tensorflow保存模型的3种方式的资源汇总
    利用率统计脚本
  • 原文地址:https://www.cnblogs.com/simple9495/p/3398829.html
Copyright © 2011-2022 走看看