6: output reverse positive integer 1-4

6 output reverse positive integer 1-4

Author: Turbo Time limit: 1S Section: branch structure

Problem Description:
Enter a positive integer not more than 4, and print out the required number on the respective bits in reverse order, for example, the original number 23, 32 to be output; The original number of 8000 and 0008 to be output; If the original number of 1 , 1 is output. Testing should be the following: a number of inputs, two, three, four positive integer; should also test the following exceptions: Input negative or 0, the number of input or more than four.

Input Description:
An integer that may be zero, or it may be negative.

Output Description:
input positive integer from 1 to 4, the reverse output of the number, if it is other integers, the output "error!". When the output of the line end of the line with no spaces.

Input Example:
8000
Sample output:
0008
Code:

#include <stdio.h>
int main()
{
    int n, st[4] = {0};
    int top = -1,i=0;
    while (scanf("%d", &n) != EOF)
    {
        if (n<=0 || n>9999)
        {
            printf("error!");
        }
        else
        {
            while (n != 0)
            {
                top++;
                st[top] = n % 10;
                n /= 10;
            }
            while (i <= top)
            {
                printf("%d", st[i]);
                i++;
            }
        }
        printf("\n");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/VictorierJwr/p/12242181.html