Answer to Question #328495 in C for MAN

Question #328495

Write a C program to sort the digits of an integer in ascending and descending order using pointers. 

Note: 

Enter an integer

print the integer in ascending and descending order


For example:

Test  Input   Result
1     54387   34578
              87543
1
Expert's answer
2022-04-14T06:16:05-0400
#include <stdio.h>

int main()
{
    int number, i, j, n, temp;
    int digits [10];
    int *p = digits;
    printf("Enter number: ");
    scanf("%d", &number);
    n = 0;
    while (number != 0)
    {
        *(p+n) = number % 10;
        number = (int)(number / 10);
        n++;    
    }
    printf("\nRezult:\n");
    for (i = 0; i < n-1; i++)
        for(j = i+1; j < n; j++)
            if (*(p+i) > *(p+j))
            {
                temp = *(p+i);
                *(p+i) = *(p+j);
                *(p+j) = temp;
            }
    for (i = 0; i < n; i++)        
        printf("%d", *(p+i));
    printf("\n");
    
    for (i = 0; i < n-1; i++)
        for(j = i+1; j < n; j++)
            if (*(p+i) < *(p+j))
            {
                temp = *(p+i);
                *(p+i) = *(p+j);
                *(p+j) = temp;
            }
    for (i = 0; i < n; i++)        
        printf("%d", *(p+i));
    printf("\n");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS