since no one replied to my first posting I am trying again. This time I was able to take a set length (4) number and sort its digits in order. Example make 5961 output 1659. Can anyone tell me how to do this without limiting the length of the number?

Please help

#include <iostream>
using namespace std;

void main()
int num;
int digit1, digit2, digit3, digit4; 
int rem1, rem2, rem3, rem4; 
int temp;

cout << "Please enter a 4 digit number: ";
cin >> num;

rem4 = num % 10;
rem3 = num % 100;
rem2 = num % 1000;
rem1 = num % 10000;

digit4 = rem4;
digit3 = rem3/10;
digit2 = rem2/100;
digit1 = rem1/1000;

int array[]={digit1, digit2, digit3, digit4};
	for(int i=0; i<4-1;i++)
		for(int j=i+1; j<4;j++)
			cout<<"\nthe sorted array is:  ";
			for(i=0; i<4; i++)

Recommended Answers

All 7 Replies

#include <iostream>
using namespace std;

int main()
   int num;
   cout << "Please enter a number: ";
   cin  >> num;
   while ( num )
      cout << num % 10;
      num /= 10;
   cout << endl;
   return 0;

Thank you for your reply, but this program will only reverse the numbers entered. How do I sort them so the output is in order from smallest to largest?

To sort the digits, first store each digit in an array and then use any of the sorting techniques to sort them in ascending order.

How do I create an array with out knowing the size or with out asking the user to enter the size?

This comes to mind.

int digits = ceil(log10(num));

I don't understand how to impliment the above code. digits is of int type. I can't use it as an array.

Have you been introduced to new?

Or do you know that there will be a specific finite number of digits that a given platform will require to represent the text of an int value? For example, a 32-bit int that has a maximum value of 2147483647 will never require more than 10 digits.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.