Regroup in reverse order

mission details

For this level, please complete a small program that recombines an integer into another integer output in reverse order.

related information

Basic arithmetic operators:
+: is to add two data to get the sum;

-: is to subtract two data to get the difference;

*: is to multiply two data to get the product;

/: is to divide the two data to get the quotient;

%: Divide two data to get the remainder.

Description:

For operators of the same priority, the order of operations is determined by the combination direction. Simply remember:
! > 算术运算符 > 关系运算符 > && > || > 赋值运算符

Programming requirements

The specific tasks are as follows:

Write a program, input a 4-digit integer, and recombine its bits into another integer output in reverse order.

The input value is a 4-digit integer, and the output value is also a 4-digit integer, but the order of numbers is reversed.

Note:
Enter an integer that is less than 4 digits and fill it with 0 (for example: 12 → 0012); if you
enter an integer that exceeds 4 digits, the first 4 digits will be truncated and the operation will be performed (for example: 123456 → 1234).
Test input:

2534

Expected output:

4352

code show as below

#include <stdio.h>
int main()
{
    
    
    printf("请输入一个4位整数:");
    int num;
    scanf("%4d",&num);
    int thousand=0,hundred=0,ten=0,bit=0,newnum;
    bit=num%10;//分离个位
    ten=num/10%10;//分离十位
    hundred=num/100%10;//分离百位
    thousand=num/1000%10;//分离千位
    newnum=bit*1000+ten*100+hundred*10+thousand;//实现重组
    printf("重新组合后:%d",newnum);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51705589/article/details/112981337