C語言:判斷一個數是否為迴文數(迴文數也是一個數字,數位特點是正反序是同一個數字,如:12321,3443)__C語言

來源:互聯網
上載者:User

迴文數的正反序是同一個數字,所以我們把這個數位高低位交換,即1234->4321 ,然後再用新的得到的數字與以前的數字交換比較是否相等,即可以判斷這個數是否為迴文數。注意:負數不是迴文數。

//判斷一個數是否為迴文數(迴文數也是一個數字,數位特點是正反序是同一個數字,如:12321,3443)#include<stdio.h>#include<stdlib.h>#include<assert.h>int if_palindrome_num(int n){assert(n > 0);int num = n;int res = 0;do{res = res * 10 + num % 10;num = num / 10;} while (num);if (res == n){return 1;}return 0;}int main(){int n = 12321;int temp = 0;temp = if_palindrome_num(n);if (temp == 0){printf("The %d is not palindrome number.\n ",n);}else{printf("The %d is palindrome number.\n ",n);}system("pause");return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.