Question
Input an integer in reverse order.
Question objective
The objective of this question is to allow beginners to use mathematical thinking to solve problems in practical use, and to gradually master the knowledge points and related skills needed to answer this question through this question, then, by analyzing the problem-solving ideas step by step, you can write them by yourself.Code.
Question Analysis
This topic is intended to allow users to input an integer (positive integer) and then output in reverse order through computer operations. For example, after the input: 53574 operation, 47535 is obtained.
Skill Requirements
Variable, data type, operator, input/output, while loop statement
Skill Review
Variable: a variable is a piece of memory space named after it is killed.ProgramThe amount of changes that can occur during running. A variable can be regarded as a container. The data stored in the container is determined by the data type that defines the variable.
Data Type: the data type is a set of values and a group of operations defined on the value set.
Operator: The operator is used to execute code operations. operations are performed on more than one operand project.
Input and Output: the console class is used for console input.
While loop statement: the while loop statement is: determines whether to execute the Loop Based on the true and false of the current condition. If it is true, false is not executed, this condition is usually changed under certain conditions during execution.
Solutions
After learning the knowledge points used in this question, let's take a look at how to solve it.
Perform a simple analysis on this question:
Enter an integer, for example, 98467, and the output result is 76489. How can this number be output in reverse order?
First, let's take a look at this number: 98467. How can we get the last digit 7 ??
Some people want to put it in a string and take the last digit of the string. However, I must declare that this question should be written in mathematics.
... ...
Let's think about how to get the last 7 in an integer ??
A simple prompt is displayed: 5/2 =? Some people immediately said: equal to 2; so how can we make this equation equal to 1 ??
... Never forget the % (modulo operator. Eventually 5% 2 = 1;
In this way, we can get 7 in 98467 to make 98467% 10000, 98467%, 1000, 98467%, or 100 10;
Is it % 10000? % 100 or something else ???
For example, what if the user inputs a double digit: 23 and 3? It must not be % 100, but 10.
In this case, we can find a regular rule. Any array % 10 can retrieve its single-digit number.
Well, we have successfully obtained the first digit. How can we retrieve the top 10 digits?
98467 how do I retrieve the number 6? Let's think that 7 has already been taken out, isn't it necessary? What is the number after 7 is deleted ?? 9846
Likewise: 9846% 10 = 6
It can be seen that this is a simple loop. So what are the conditions for cyclic termination ?? Think about how much is the number after we take out all the digits of the number 98467 ??
The answer is: 0; if this number is changed to 0, it is the condition for loop termination.
Code steps
1. Define two variables to store the input integers and the retrieved numbers respectively.
2. Obtain the input integer from the interface
3. cyclically output computation results
Code details
// Define two integers to store the input numbers and output results respectively. Int Num, result; console. writeline ( " Enter the number to be output in reverse order " ); Num = Int . Parse (console. Readline ()); // Cyclic computing ends until the input number is 0. While (Num! = 0 ){ // First retrieve the last digit // Enter this digit. // Finally, convert the current integer to the integer that loses the last digit. Result = num % 10 ; Console. Write (result); num /= 10 ;} Console. readkey ();
Running Effect
Author:Memories of lost youthSource:Http://www.cnblogs.com/lukun/The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is displayed at an obvious location on the page. If you have any problems, you can useHttp://www.cnblogs.com/lukun/ Thank you very much for contacting me.