# Chapter 2 programming questions - C language programming: modern methods (2nd Edition) after class answers

2.1

Write a program and use printf to display the following graphics on the screen: ```#include <stdio.h>

int main(void) {

printf("       *\n");
printf("      * \n");
printf("     *  \n");
printf("*   *   \n");
printf(" * *    \n");
printf("  *     \n");

return 0;
}```

2.2

Write a program to calculate the volume of the sphere, where the radius of the sphere is 10 m, and refer to the formula v = 4/3  r 3. Note that the score of 4 / 3 should be written as 4.0f/3.0f. (what happens if the score is written as 4 / 3?) Tip: C language has no exponential operator, so you need to multiply r twice to calculate r 3.

```#include <stdio.h>

int main(void) {

printf("Sphere volume: %.2f cubic meters\n", 4.0f/3.0f * 3.14f * 1000);
return 0;
}```

2.3

Modify the program in the above question so that users can enter the radius of the sphere by themselves.

```#include <stdio.h>

int main(void) {

int r = 0;

scanf("%d", &r);

printf("\nSphere volume: %.2f cubic meters\n",
4.0f/3.0f * 3.14f * r * r * r);
return 0;
}```

2.4

Write a program that asks the user to enter a dollar amount, and then displays the corresponding amount after increasing the tax rate by 5%. The format is as follows:
Enter an amount: 100.00

```#include <stdio.h>

int main(void) {

float money = 0.0f;
printf("Enter an amount: ");
scanf("%f", &money);
printf("With tax added: \$%.2f\n", money * 1.05f);

return 0;
}```

2.5

Programming requires the user to input the value of x, and then display the value of the following polynomial: Tip: C language has no exponential operator, so you need to multiply x to calculate its power. (for example, x*x*x is the third power of X.)

```#include <stdio.h>

int main(void) {

int x = 0;

printf("Enter value for x: ");
scanf("%d", &x);
printf("Result: %d\n",
(3 * x * x * x * x * x) + (2 * x * x * x * x) - (5 * x * x * x)
- (x * x) + (7 * x) - 6);

return 0;
}```

2.6

Modify the above question and evaluate the polynomial with the following formula:

((((3x + 2) x - 5)x - 1)x + 7)x - 6
Note that the number of multiplications required by the modified program is reduced. This polynomial evaluation method is Horner's Rule.

```#include <stdio.h>

int main(void) {

int x = 0;

printf("Enter value for x: ");
scanf("%d", &x);
printf("Result: %d\n",
((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6);

return 0;
}```

2.7

Write a program that asks the user to enter a dollar amount, and then shows how to use a minimum of \$20, \$10, \$5 and \$1
Payment in USD:
Enter a dollar amount: 93
\$20 bills: 4
\$10 bills: 1
\$5 bills: 0
\$1 bills: 3
Tip: divide the payment amount by 20 to determine the amount of \$20, and then subtract the total amount of \$20 from the payment amount. Repeat this operation for notes of other denominations. Make sure you always use integer values in your program, not floating-point numbers.

```#include <stdio.h>

int main(void) {

int money = 0;

printf("Enter a dollar amount: ");
scanf("%d", &money);

printf("\$20 bills: %d\n", money/20);
money -= 20 * (money/20);

printf("\$10 bills: %d\n", money/10);
money -= 10 * (money/10);

printf(" \$5 bills: %d\n", money/5);
money -= 5 * (money/5);

printf(" \$1 bills: %d\n", money);

return 0;
}```

2.8

Program and calculate the remaining loan amount after repayment in the first, second and third months:
Enter amount of loan: 20000.00
Enter interest rate: 6.0
Enter monthly payment: 386.66
Balance remaining after first payment: \$19713.34
Balance remaining after second payment: \$19425.25
Balance remaining after third payment: \$19135.71
Keep two decimal places when displaying the balance after each repayment. Tip: after deducting the repayment amount from the loan balance of each month, you also need to add the product of the loan balance and the monthly interest rate. The monthly interest rate is calculated by converting the interest rate entered by the user into a percentage and dividing it by 12.

```#include <stdio.h>

int main(void) {

float loan = 0.0f,
rate = 0.0f,
payment = 0.0f;

printf("Enter amount of loan: ");
scanf("%f", &loan);

printf("Enter interest rate: ");
scanf("%f", &rate);

printf("Enter monthly payment: ");
scanf("%f", &payment);

loan = loan - payment + (loan * rate / 100.0 / 12.0);
printf("Balance remaining after first payment: \$%.2f\n", loan);

loan = loan - payment + (loan * rate / 100.0 / 12.0);
printf("Balance remaining after second payment: \$%.2f\n", loan);

loan = loan - payment + (loan * rate / 100.0 / 12.0);
printf("Balance remaining after third payment: \$%.2f\n", loan);

return 0;
}```

Tags: FPGA

Posted by Rippie on Tue, 17 May 2022 04:24:13 +0300