Experiment 2-1-7 Each digit of the integer 152 (10 points)

This question requires writing a program to output the values ​​of the units digit, tens digit and hundreds digit of the integer 152.

Input format:

There is no input for this question.

Output format:

Output in the following format:

152 = 个位数字 + 十位数字*10 + 百位数字*100

Code:

# include <stdio.h>
# include <stdlib.h>

int main(){
    
    
    int num = 152;
    int alone,ten,hundred;
    hundred = num / 100;
    ten = (num % 100) / 10;
    alone = num - 100 * hundred - 10 * ten;
    printf("152 = %d + %d*10 + %d*100",alone,ten,hundred);
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

This question mainly examines the operation of integer 取余and 除法. As the name implies, taking the remainder is to obtain the remainder of this number. The title says to get the three-digit one, ten, and hundred digits. We know that the hundred digit is the first of this number. For a letter, we divide this number (num) by 100, and the number we get is the number with the hundreds digit (for example: 321/100 = 3). There are actually two ways of thinking about the tens number:

  • ① Using num-100 * to get the hundred digits, the result is the sum of the tens digits and the ones digits, and then the idea is much clearer, and the result obtained by directly dividing this number by 10 is the tens digits, and the last ones digits The numbers are ready to come out!
  • ② Use the remainder operation. After num and 100 are used to get the remainder, the tens and ones digits are obtained. The rest of the operation is similar to ①. This question uses this idea

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114363936