Latest web development tutorials

C Exercise Example 29

100 cases of classic C language 100 cases of classic C language

Topic: no more than five to a positive integer, requirements: First, it is seeking few, two, reverse print out the digits.

Program analysis: decomposition of each digit to learn, as explained below.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include <stdio.h>

int main( )
{
    long a,b,c,d,e,x;
    printf("请输入 5 位数字:");
    scanf("%ld",&x);
    a=x/10000;        /*分解出万位*/
    b=x%10000/1000;   /*分解出千位*/
    c=x%1000/100;     /*分解出百位*/
    d=x%100/10;       /*分解出十位*/
    e=x%10;           /*分解出个位*/
    if (a!=0){
        printf("为 5 位数,逆序为: %ld %ld %ld %ld %ld\n",e,d,c,b,a);
    } else if(b!=0) {
         printf("为 4 位数,逆序为: %ld %ld %ld %ld\n",e,d,c,b);
    } else if(c!=0) {
         printf("为 3 位数,逆序为:%ld %ld %ld\n",e,d,c);
    } else if(d!=0) {
         printf("为 2 位数,逆序为: %ld %ld\n",e,d);
    } else if(e!=0) {
         printf("为 1 位数,逆序为:%ld\n",e);
    }
}

The above example output is:

请输入 5 位数字:12345
为 5 位数,逆序为: 5 4 3 2 1

100 cases of classic C language 100 cases of classic C language