Use while to determine whether an input number is a simple implementation of a palindrome number

  • 2020-04-02 02:13:54
  • OfStack



# include <stdio.h> 
int main(void)
{
    int m,val,sum = 0;
    printf(" Please enter a palindrome number, if palindrome number is returned YES Otherwise return No : ");
    scanf("%d",&val);
    m = val;    
     while(m)
     {
         sum = sum*10+m%10;
         m/= 10;
     }
    if(sum == val)
    printf("YESn");
    else
    printf("NOn");

}

      // perform flow analysis on the while statement
      // assume that the user enters the number 121, and then judge whether the palindrome number is true if m is not 0, otherwise it is false, and output YES if true, and output No if false;

      1 - > Sum = sum*10+m%10 (sum =0 *10+121%10)//sum=0+1, sum=1
                    M / = 10 (m=m/10) //m=121/10, m=12
      2 - > Sum = sum * 10 + 10 m % (12% sum = 1 * 10 + 10) / / sum = 10 + 2, sum = 12
                  M /10 =10(m=m/10) //m=12/10, m=1
      3 - > Sum = sum*10+m%10(sum=12*10+1%10)//sum=120+1, sum=121
                M /10 =10(m=m/10) //m=1/10, m=0

M = 0 is false, then output No


Related articles: