Write a program in c language to print/calculate the number of 9 in the integer of 1-100

Table of contents

Topic analysis:

Code:

operation result 

expand:


Topic analysis:

To count/calculate the number of 9 in 1-100, so we can consider using for loop to generate the number of 1-100

The numbers with 9 are: 9 19 29 39 49 59 69 79 89 90 91 92 93 94 95 96 97 98 99

There are a total of 19 numbers. According to observation, it is easy to find that among the previous numbers, the numbers with 9 appearing in the tens place, and the 99 digits have the tens place. Therefore, we can use the condition if (i % 10 == 9 || i / 10 == 9) Judgment, set a counter to count the number of times, increase by 1 after the condition is met, and finally print the number of times (printed outside the loop) as shown in Figure 1 or print the number with 9 (printed inside the loop) as shown in the figure two

Code:

Calculation times

                                                                        Figure 1

Figure II

operation result 

expand:

We can also use the same method to calculate and print the numbers that appear 7, 6, 5, etc.

Guess you like

Origin blog.csdn.net/2301_77509762/article/details/130379063