C Language Programming Exercise Explanation

C Language Programming Exercise ExplanationC Language Programming Exercise ExplanationC Language Programming Exercise ExplanationC Language Programming Exercise ExplanationC Language Programming Exercise Explanation

C Language Exercise Analysis

These problem-solving ideas will enlighten you✨

In the world of programming, C language is like a mysterious and challenging mountain. Whether you are a novice stepping into the programming door or a developer who has been swimming in the sea of code for a long time, you will always encounter various difficulties in learning and practicing C language. Today, let’s delve into the problem-solving ideas of several classic C language problems to help you easily overcome the “stumbling blocks” on your programming journey!

Drawing a RectangleC Language Programming Exercise ExplanationC Language Programming Exercise Explanation

“Drawing a rectangle” is a problem answered by student Chen Junheng.

1. First, look at the input format: four values need to be input, which are integer, integer, string, and integer.

2. First, determine whether the rectangle is hollow or solid, and write them separately. Use if-else statements to make the judgment.

3. If it is hollow (lines 9-30 in the figure), first use loops to control the height and width, then nest a for loop within another for loop to control the height and width respectively, and finally add a newline character after the width control loop ends. For the hollow part, it is necessary to determine whether the rows and columns to be filled are the first row (column) or the last row (column). If so, fill with symbols; if not, fill with spaces (using if-else statements).

4. If it is not hollow (lines 32-43 in the figure), the content is the same as the previous one, except that spaces do not need to be input (first use loops to control the height and width, then nest a for loop within another for loop to control the height and width respectively, and finally add a newline character after the width control loop ends).C Language Programming Exercise ExplanationC Language Programming Exercise ExplanationCalculating FactorialC Language Programming Exercise Explanation

“Calculating factorial” is a problem answered by student Gao Jiawei.

For this problem, he said: define a factorial function with two cases; when the number equals 1, return 1 directly; when the number is not equal to 1, multiply it by this function (for example: return x * f(x-1)), which can be used for factorial, and then directly write the program using the function.

C Language Programming Exercise ExplanationC Language Programming Exercise ExplanationNumber CountingC Language Programming Exercise Explanation

“Number counting” is a problem answered by student Zhang Wenting.

1. First, define three integer variables a, b, c, and let b=0, where b is used to count the occurrences of 2.Use the scanf function to get two integers, int i=a initializes the loop variable i to the left endpoint value a, i<=c is the loop condition, if i is less than or equal to the right endpoint value c, it will execute i++, that is, i plus 1.

2. In each iteration of the outer loop, assign the value of i to a new variable d to ensure that i is a fixed value. If d>0, it will continue to enter the loop, d%10 is used to obtain the last digit of d, and check if it is 2; if it is 2, b is incremented by 1. d/10 is used to remove the last digit of d, checking if the previous digit is 2, this step continues until d is 0.

3. Then use the printf function to output the value of b, which is the number of times the digit 2 appears in that interval.

C Language Programming Exercise ExplanationC Language Programming Exercise ExplanationENDC Language Programming Exercise ExplanationInformation A Class Competition AssociationEditor: Zhang Mengmeng

Editor-in-chief: Wang Yijia

Reviewed by: Tian Jianxue

Leave a Comment