c Question 1: Read an integer number from the keyboard, calculate how many digits the number has, and print out the number in each digit.

Read an integer number from the keyboard, count how many digits the number has, and print out the number in each digit.

  1. Loop and remainder operator (pow)
  2. Because we need to use pow, we need to add #include to introduce the math library.
  • Start writing code
    #include <stdio.h>
    
    int main() {
        int num, count = 0, temp;
        printf("Please enter an integer:");
        scanf("%d", & amp;num);
        
        temp = num;
        
        // Handle the case of 0
        if (temp == 0) {
            count = 1;
            printf("The number has %d bits\\
    ", count);
            printf("The number on each digit is: %d\\
    ", temp);
            return 0;
        }
        
        // Calculate the number of digits
        while (temp != 0) {
            temp /= 10;
            count + + ;
        }
        
        //Number of printing digits
        printf("The number has %d bits\\
    ", count);
        
        //Print each digit
        printf("The number on each digit is:");
        while (num != 0) {
            int digit = num % 10;
            printf("%d ", digit);
            num /= 10;
        }
        
        return 0;
    }
    
    

    Let’s analyze the code step by step

  • int num, count = 0, temp; First, set the three variables num, count, and temp, where the initial values of num and count are 0, and secondly, they are all integers. count is used to calculate the number of digits, and num is used to read the variable entered on the keyboard.

  • printf(“Please enter an integer:”);

    scanf(“%d”, & amp;num); This paragraph can be imagined as a computer saying: Hey, you should enter an integer. Then you tell the computer the integer you want to input and use the scanf function to complete it.

    `scanf(“%d”, & amp;num);` is an input function in C language, used to read an integer from the standard input (keyboard) and store it in the variable `num`.

    Here, the `scanf` function uses the format string `”%d”`, indicating that a decimal (decimalism) integer is to be read. `&num` means saving the read integer into the memory address of variable `num`. The & amp; symbol is an address symbol, used to obtain the memory address of a variable.

    Simply put, what this line of code does is enter an integer from the keyboard and store the integer into the `num` variable for subsequent use.

  • temp = num; Let the variable temp be the same as num.

  • // Handle the case of 0

    if (temp == 0) {

    count = 1;

    printf(“The number has %d bits\\
    “, count);

    printf(“The number on each digit is: %d\\
    “, temp);

    return 0;

    }

    This code illustrates a special case, which is 0

  • // Calculate the number of digits

    while (temp != 0) {

    temp /= 10;

    count + + ;

    }Using a small loop, while (newly learned, hehe)

    To illustrate, let’s say the integer we input is 367.

  • Initially, the value of temp is equal to 367.
  • In the first loop, the result of dividing temp by 10 is 36, and assign it back to temp. At this time, the value of temp is 36 , is not equal to 0, so the loop continues.
  • In the second loop, the result of dividing temp by 10 is 3, and assign it back to temp. At this time, the value of temp is 3 , is not equal to 0, so the loop continues.
  • The third time through the loop, the result of dividing temp by 10 is 0

    In C language, integer division will truncate the result to an integer and the result will not contain the decimal part.

    If you want to obtain a floating-point result, you need to mark at least one of the operands as a floating-point number, for example, change one of the numbers in the numerator or denominator to a floating-point number.

    and assign it back to temp. At this time, the value of temp becomes 0, which is equal to 0, so the loop ends. From above Picture, I have a doubt: why a string like “the number on each digit is:” should be written outside the loop instead of inside the loop. After experiments, we can clearly see different results (chatgpt won again day), these statements inside the loop will also loop, so it’s not very good. Therefore it should be placed outside the statement.

  • int digit = num % 10;

    printf(“%d “, digit);

    num /= 10; In the second loop, we print the number on each digit. We use an additional variable digit to save the current number of digits. In each loop, we use the remainder operation of num divided by 10 (num % 10) to get the number at the current position, and use printfThe function prints it out. We then divide num by 10 so that the next loop can handle the next digit. (%d appears again, remember what it is? Decimal!) num=num/10

  • Finally, we use the return 0 statement to indicate that the program ends normally.

    So, can you still type the code yourself? Come and urge me to type the code again! (Because I really want to be a boss)

    First introduce #include

    The art of rerurn 0: In the main function, as long as the return statement is executed, the program will terminate and return the corresponding return value. In this case, return 0 indicates that the program executed successfully and returned 0 as the return value.

  • Me again I made a mistake. The relationship between temp and num is not clear. Temp is a changing quantity, but num is different. The value of num cannot be changed, so you need to write num=temp to let temp continue to change.

    #include <stdio.h>
    
    //Call int main function
    int main(){
        //Set the required variables, we currently need 3
        int num=0,count=0,temp;
        temp=num;
        //You also need prompt commands entered by the computer, remember the semicolon! What is %d? It’s decimal! Assign the value entered on the keyboard to num
        //The first error occurred, that is, printf and scanf were not understood clearly.
        //printf is what you want the computer to display on the screen, scanf is what you tell the computer using the keyboard
        //So you need to add a printf
        // & amp; is very important, used to pass the address
        printf("Enter an integer:");
        scanf("%d", & amp;num);
        
        //Damn, the problem arises, if no longer does not add parentheses and colons like python, but becomes
        //if (expression){}, need to remember
        //Can't run currently, always reporting an error
        //Consider the first case, which is 0
        if (num==0){
            printf("The number has 1 digit, and the number in each digit is 0");
            return 0;
            };
        
        //In the second case, to calculate how many bits there are, a loop is used
        //Integer, so there are no digits after the decimal point after integer division
        while (temp!=0){
            num/=10;
            count + + ;
            
        }
    
        
        
        return 0;
        //To prevent forgetting, write it down first
    }
    
    

    This is the code that is currently typed, and there are many problems.

  • There are several errors in the code:

  • int whlie (temp!=0) should be while (temp!=0), change whlie to while >. (The code above has been modified)

  • num /= 10; should be temp /= 10;, this is to update the value of temp in the loop instead of num .

  • return 0; should be after the entire loop ends, not on the first iteration of the loop. Move the return 0; statement outside the loop. There is no need to return 0 in this question of while loop

  • In the first case (num==0), the part to print each digit is missing. A print statement needs to be added.

  • Question: Why can the subsequent count and digit be assigned values without the & amp; symbol?

  • The address-getting symbols ( & amp;) are mainly used to get the address of a variable, but not to get the value of the variable. In the above code, & amp; is used in the scanf function to obtain the address of the variable num, because we want to move from the keyboard The entered value is stored in the memory location of num. In contrast, the memory addresses of the count and digit variables are already determined when they are defined, and we just store the values into these in the memory locations of variables instead of getting their addresses.

  • Error: implicit declaration of function ‘whlie’ [-Wimplicit-function-declaration]

    Implicit function declaration meansthat the function is called directly without explicit declaration of the function or definition of the function prototype before using the function. When the compiler encounters this situation, it willassume that the function has a return type of int and accepts any parameter type, and issues a warning that the function may not be is declared correctly.

    In the code, an implicit declaration error of function ‘whlie’ occurs because there is no explicit declaration of the function or definition of the function prototype before using the whlie function.

    To solve this problem, you can tell the compiler about the function by adding a function prototype or explicit declaration before the function call. For example, if ‘whlie’ is a custom function, you can add a function prototype or declaration before the function call: int whlie(int temp);

    This way the compiler can correctly identify the ‘whlie’ function and match it to a function call at compile time. Ensuring that you provide the correct function declaration before a function call can avoid implicit function declaration warnings and ensure the correctness of your code.

    #include <stdio.h>
    
    //Call int main function
    int main(){
        //Set the required variables, we currently need 3
        int num=0,count=0,temp,digit;
        temp=num;
        //You also need prompt commands entered by the computer, remember the semicolon! What is %d? It’s decimal! Assign the value entered on the keyboard to num
        //The first error occurred, that is, printf and scanf were not understood clearly.
        //printf is what you want the computer to display on the screen, scanf is what you tell the computer using the keyboard
        //So you need to add a printf
        // & amp; is very important, used to pass the address
        printf("Enter an integer:");
        scanf("%d", & amp;num);
        
        //Damn it, the problem arises, if no longer does not add parentheses and colons like python, but becomes
        //if (expression){}, need to remember
        //Can't run currently, always reporting an error
        //Consider the first case, which is 0
        if (num==0){
            printf("The number has 1 digit, and the number in each digit is 0");
            return 0;
            };
        
        //In the second case, to calculate how many bits there are, a loop is used
        //Integer, so there are no digits after the decimal point after integer division
        while (temp !=0 ){
            temp/=10;
            count + + ;
        }
        printf("The number of digits is: %d\\
    ",count);
    
        //Third, the number on each bit, loop
        printf("The digits on each bit are:");
        while (temp != 0){
            digit==temp;
            printf("%d",digit);
            num/=10;//
        }
         return 0; //To prevent forgetting, write it down first
    }
    
    

    This code has many problems and the final result is outrageous, but I really can’t find the answer.

This is not the answer I want. . .

  • question:
  • 1.The location problem of temp =num;

  • answer:

    Because executing temp=num; before line 7 means that the initial value of temp is also 0

  • 2. Why do you need to use num instead of temp in the second loop?
  • Answer:

    In the first loop, the value of temp has been divided by 10 and becomes 0. So if you use temp in the second loop, the loop will not execute any number of times.

    Make sure that temp still retains the initial value of num after the first loop by placing temp = num; before line 16, Thus iterating with the correct values in the second loop.

  • Why should we add num/=0 in the second loop;
  • Answer: To ensure that we don’t process any number twice, we need to divide num by 10 after each iteration in order to process the next digit of the number in the next iteration.
  • int digit = num % 10;: This line of code means to get the last digit of num
  • % divides num by 10 and returns the remainder of the division operation, which is the single-digit number of num.
    #include <stdio.h>
    
    //Call int main function
    int main(){
        //Set the required variables, we currently need 3
        int num=0,count=0,temp,digit;
        
        //You also need prompt commands entered by the computer, remember the semicolon! What is %d? It’s decimal! Assign the value entered on the keyboard to num
        //The first error occurred, that is, printf and scanf were not understood clearly.
        //printf is what you want the computer to display on the screen, scanf is what you tell the computer using the keyboard
        //So you need to add a printf
        // & amp; is very important, used to pass the address
        printf("Enter an integer:");
        scanf("%d", & amp;num);
        //The position of this is very important. When I write it on line 7, the output is not the result I want, but when I write it on line 16, it succeeds.
        //Because temp=num; is executed before line 7, it means that the initial value of temp is also 0 bits
        temp=num;
        //Damn, the problem arises, if no longer does not add parentheses and colons like python, but becomes
        //if (expression){}, need to remember
        //Can't run currently, always reporting an error
        //Consider the first case, which is 0
        if (num==0){
            printf("The number has 1 digit, and the number in each digit is 0");
            return 0;
            };
        
        //In the second case, to calculate how many bits there are, a loop is used
        //Integer, so there are no digits after the decimal point after integer division
        while (temp !=0 ){
            temp/=10;
            count + + ;
        }
        printf("The number of digits is: %d\\
    ",count);
    
        //Third, the number on each bit, loop
        printf("The digits on each bit are:");
        while (num != 0){
            int digit=num;
            printf("%d ",digit);
            num/=10;//When I deleted this line, I found that it had an infinite loop, which was scary.
            //But I don’t know why this is so.
            //To ensure that we don't process any one number twice, after each iteration, divide num by 10 so that the next digit of the number is processed in the next iteration.
        }
         return 0; //To prevent forgetting, write it down first
    }
    
    

    This is the final code. It finally took me half a day to understand it completely! I would like to thank gpt and the group of friends for their help. The process was difficult, but in the end I was so happy that I finally finished it all, which gave me courage!

    #include <stdio.h>
    int main(){
        int num=0,count=0,temp,digit;
        printf("Enter an integer:");
        scanf("%d", & amp;num);
        temp=num;
        //Consider the first case, which is 0
        if (num==0){
            printf("The number has 1 digit, and the number in each digit is 0");
            return 0;
            };
        //In the second case, to calculate how many bits there are, a loop is used
        while (temp !=0 ){
            temp/=10;
            count + + ;
        }
        printf("The number of digits is: %d\\
    ",count);
        //Third, the number on each bit, loop
        printf("The digits on each bit are:");
        while (num != 0){
            int digit=num;
            printf("%d ",digit);
            num/=10;
        }
         return 0;
    }
    
    

    The above is the final simplified code, friends who need it can get it themselves.