以下程式碼供大家參考
方法1
#include <stdio.h> #include <stdlib.h> int reversing(long int); int reversing(long int number) { while(number > 0) { printf("%d",number%10); number=number/10; } return 0; } void main (void) { long int number; printf("please enter number > "); scanf("%d",&number); printf("The reversing of %d is ",number); reversing(number); printf("\n"); system("pause"); }方法2
#include <stdio.h> #include <stdlib.h> int reversing(long int); int reversing(long int number) { long int result=0,temp,ten_pow=1; int k=0; temp=number; //計算次數前先把數字存起來 //--------計算最大是十的幾次方-------- while(number > 0) { number=number/10; k++; } //--------計算剛剛的十的N次方是多少 number=temp; for(int i=1;i<k;i++) ten_pow=ten_pow*10; //-------逐一取餘數,並乘上十的N次方或N-1次方等等 while(number > 0) { result=result+((number%10)*ten_pow); number=number/10; ten_pow=ten_pow/10; } //傳回結果 return result; } void main (void) { long int number; printf("please enter number > "); scanf("%ld",&number); printf("The reversing of %ld is %d",number,reversing(number)); printf("\n"); system("pause"); }
沒有留言:
張貼留言