C language: Input an integer, output in reverse order (while loop implementation) and improve it with the method of construction

#include<stdio.h>
int main(){
//	输入一个整数,逆序输出 
     int num;
	 scanf("%d",&num); 
	 while(num!=0){
	 	printf("%d",num%10);
	 	num/=10;
	 }
	return 0;
}

The idea of ​​the above code is to use the remainder operation to calculate the single digit and output

Next, we refer to some simple algorithms to improve the above program (construction method)

Idea: ones digit + tens digit * 10 + hundreds digit * 100 + thousands digit * 1000+......

#include<stdio.h>
int main(){
//	输入一个整数,逆序输出 
//	构数法改进 
	int num,sum=0;
	 scanf("%d",&num); 
	 while(num!=0){
	 	sum=sum*10+num%10;
	 	num/=10;
	 }
	 	printf("%d",sum);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_63987141/article/details/129150205