• 分支和循环


    1.

    #include<stdio.h>
    int main(void)
    {
        double num1,num2;
        char op;
    
        printf("Type in an expression:");
        scanf("%lf%c%lf",&num1,&op,&num2);
    
        if(op=='+')
            printf("=%.2f\n",num1+num2);
        else if(op=='-')
            printf("=%.2f\n",num1-num2);
        else if(op=='*')
            printf("=%.2f\n",num1*num2);
        else if(op=='/')
            printf("=%.2f\n",num1/num2);
        else
            printf("Unknown operator\n");
    
        return 0;
    
    }

    2.

    #include<stdio.h>
    int main(void)
    {
        
        double x,y;               
         
        printf("请输入电量:\n");
        scanf("%Lf",&x);
        if(x<0){
               printf("sorry,erro");
            }
            else  if(x<=50){
                    y=0.53*x;
            }
            else{
                 y=50*0.53+(x-50)*0.58;
            }
                printf("y=%.3f\n",y);
            
            return 0;
         }

    3.

    #include <stdio.h> 
    int main() 
    { 
        double x,pow = 1.0; 
        int i,n; 
       
        printf("Enter x and n:\n"); 
        scanf("%Lf%d",&x,&n);
        
        for(i=1;i<=n;i++)
        pow=pow*x;
        printf("pow(%Lf%d)=%.2f\n",x,n,pow);
    
        return 0;
    }

    4.

    #include<stdio.h>
    int main(void)
    {
        int m,n,i;
        double y;    
        y=0;
        printf("Enter m,n:");
        scanf("%d%d",&m,&n);
    
        for(i=m;i<=n;i++)
           y=y+(i*i+1.0/i);
            
        printf("y=%Lf\n",y);
        
    }

    5.

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

    6.

    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int month,year,loan,lower,money,upper;
        double rate;
        lower=5;
        upper=30;
        printf("Enter loan:");
            scanf("%d",&loan);
    printf("Enter rate:");
        scanf("%Lf",&rate);
        printf("Enter year:");
        for(year=lower;year<=upper;year++){
            month=year*12;
            money=(loan*rate*pow((1+rate),month))/(pow((1+rate),month)-1);
            printf("%d:  %d\n",year,money);
        }
            return 0;
        }

    7.

    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int n,i;
        double sum,item;
        sum=0;
        printf("Enter n:");
        scanf("%d",&n);
    
        for(i=1;i<=n;i++){
            item=powl(2,i);
            sum=sum+item;
        }
        printf("sum=%Lf\n",sum);
    
        return 0;
    }

  • 相关阅读:
    swoole学习(四):websocket
    LeetCode--SQL 查询:体育馆的人流量
    LeetCode--SQL 查询:有趣的电影
    centos7下mysql5.7忘记密码
    LeetCode--SQL 查询:删除重复的电子邮箱。
    swoole学习(三):HTTP服务
    swoole学习(二):TCP/UDP服务器-客户端
    swoole学习(一):初识swoole
    LeetCode--SQL 查询:查找部门工资前三高的职员
    报文、报文段、分组、包、数据报、帧、数据流的概念区别
  • 原文地址:https://www.cnblogs.com/tangxumin25/p/3379614.html
Copyright © 2020-2023  润新知