C language - Integer inversion (an example of 4, 4 loops in the fourth week of C language introduction by Professor Weng Kai of Zhejiang University)

Ideas

Large framework + read user input + define the input value, take the remaining value, and return value = 0

while loop, loop body: first take the remainder value (that is, the ones to be taken out), and calculate the return value (in reverse order) [Method: Multiply by 10 and add the remainder value]

Output: X is left, the remainder value is taken, and the return value is

code

#include<stdio.h>
int main()
{
    
    
	int x;
	scanf("%d",&x);// 读取 用户输入的原数 
	int digit;
	int ret=0;
	
	while(x>0)
	{
    
    
		digit =x%10;
		
		printf("%d\n",digit); 
		
		ret = ret*10 + digit; // 求逆序    乘回来+取走的数值 
		
		printf("x=%d,digit=%d,ret=%d\n",x,digit,ret);
		
		x/=10;
		
	} 
	printf("%d",ret);
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/m0_57495651/article/details/132279343