# Explicitly explain the exercises in Chapter 2 of the C language

## Section 1: Operations

Exercise 2-1: Write a program that reads two integers as shown below and displays the percent of the former
Integer X: 54
Integer Y: 84
The value of X is that of Y: 64%

```#include<stdio.h>
int main()
{
int a, b;
printf("integer X: ");
scanf("%d", &a);
printf("integer Y: ");
scanf("%d", &b);
printf("X Yes Y of%d%%", (a*100)/b);
return 0;
}
```

Exercise 2-2: Write a program that reads two integers as shown below and outputs their sum and product
Integer X: 54
Integer Y: 12
Their sum is: 66, and their product is 648.

```#include<stdio.h>
int main()
{
int a, b;
printf("integer X: ");
scanf("%d", &a);
printf("integer Y: ");
scanf("%d", &b);
printf("Their sum is:%d，Their product is:%d",a+b,a*b);
return 0;
}
```

## Section 2: Data Types

Exercise 2-3: Write a program that displays the read real value as shown below.
Please enter a real number: 57.3
The real number you entered is: 57.300000

```#include<stdio.h>
int main()
{
double a = 0;
scanf("%lf", &a);
printf("The real numbers you entered are:%lf", a);
return 0;
}
```

Exercise 2-4: Write programs to perform multiplication and division operations on integer constants, floating point constants, int variables, and double variables, and verify the rules.
Rule: When double type is operated with int type, the result will be implicitly converted to double type.

```#include <stdio.h>
int main()
{
int n1 = 20;
int n2 = 2;
double d1 = 20.0;
double d2 = 2.0;
printf("Multiply an integer constant by an integer constant:%d\n", 20 * 2);
printf("A floating-point constant is multiplied by a floating-point constant:%f\n", 20.0 * 2.0);
printf("int type constant multiplied by int Type constant:%d\n", n1*n2);
printf("double type constant multiplied by double Type constant:%f\n", d1*d2);
printf("double type constant multiplied by int Type constant:%f\n", d1*n2);
return 0;
}
``` Exercise 2-5: Write a program that reads the value of two integers as follows, calculates the former is tens of percent of the latter, and outputs it as a real number
Integer A: 54
Integer B: 84
A is B's: 64.285714%

```#include<stdio.h>
int main()
{
double a, b;
printf("real numbers A: "); scanf("%lf", &a);
printf("real numbers B: "); scanf("%lf", &b);
printf("A Yes B of%f%%", (a * 100) / b);
return 0;
}
```

Exercise 2-6: Display the integer value of the read height and the real value of the standard weight as shown below. The standard weight is calculated according to the formula (height-100) × 0.9, and the result is rounded to one decimal place.
Your standard weight is: 67.5 kg

```#include<stdio.h>
int main()
{
double a = 0;
scanf("%lf", &a);
printf("your standard weight is%.1f Kilogram", (a-100)*0.9);
return 0;
}
```

## Summary section

The operand type of the % operator must be an integer
Implicit type conversion occurs when an operation has different operands. Operands of the smaller data type are converted to operands of the larger data type.
When the printf function is used to display the value of double type, the conversion specification is %f
When the scanf function reads a value of type double, the conversion specification is %lf
(double) 5 converts operand 5 of type int to operand 5.0 of type double
Conversion description consists of 0 flag, minimum field width, precision, conversion description

```printf("09.2f");
0 Indicates the 0 flag, if the value is preceded by a blank, it is filled with 0
9 Represents a floating-point number of at least 9
.2 Indicates that only two decimal places are displayed
```

To display the % sign in the printf function, just write %%

Summary Exercise: Use all of the above in one program.

```#include<stdio.h>
int main()
{
int a, b;
double r;
printf("integer a and b The value of:");
scanf("%d%d", &a, &b);
printf("a+b=%d\n", a + b);
printf("a-b=%d\n", a - b);
printf("a*b=%d\n", a * b);
printf("a/b=%d\n", a / b);
printf("a%b=%d\n", a % b);

printf("(a+b)/2\n", (a + b) / 2);
printf("average value:%f\n\n", (double)(a + b) / 2);