MOOC Harbin Institute of Technology C language programming essence-exercises after class

Week 2-Those things between numbers, do some calculations.
Programming questions are chosen to do.
Always make mistakes: (1) Scanf & sometimes forget
(2) scanf("%lf,%d,%) lf",&rate, &year, &capital); If the type does not match, the variable cannot be read normally
(3)x1=(-3/(2.0*2.0))+sqrt(3*3-4*2*1)/ (2*2); Remember to add parentheses (2.0*2.0), and also note that if you want to get a decimal result in division, write the operand as a floating point type.
(4) For forced type conversion, see the example in the book P39
int m=5; at this time (float)m/2 can get 2.5 for type forced conversion, but (float)(m/2) can only get 2.0
at the same time, forced conversion After that, the data type of m is still an integer and has not changed.
Need to pay attention here, it is easier to make mistakes.

1 output reverse sequence number (3 points)

#include<stdio.h>
#include<math.h>
int main(){
    
    
    printf("Input x:\n");//这一行必不可少
    int x,y,x1,x2,x3;
    scanf("%d",&x);//&x forget the &
    x = fabs(x);
    x1 = x/100;
    x2 = x%100/10;
    x3 = x%10;
    y = 100*x3+10*x2+x1;
    printf("y=%d\n",y);
}

3 Deposit interest rate calculator V1.0 (3 points)

#include<stdio.h>
#include<math.h>
int main(){
    
    
    double rate, capital,deposit;
    int year;
    printf("Please enter rate, year, capital:\n");
    scanf("%lf,%d,%lf",&rate, &year, &capital);
    deposit = capital*pow((1+rate),year);
    printf("deposit=%.3f\n",deposit);
}

7 Find the roots of a quadratic equation (3 points)

#include<stdio.h>
#include<math.h>
int main(){
    
    
    double x1,x2;
    //(2*2)忘记加括号了,同时忘记写成2.0了
    x1=(-3/(2.0*2.0))+sqrt(3*3-4*2*1)/(2*2);
    x2=(-3/(2.0*2.0))-sqrt(3*3-4*2*1)/(2*2);
    printf("x1=%.4f\n",x1);
    printf("x2=%.4f\n",x2);

}

Guess you like

Origin blog.csdn.net/weixin_43919570/article/details/105307342