Thursday 14 May 2015

How to sort digits of a number in C++?

To sort digits of a number in C++ the code is as follows:

//Program to sort digits of a number in C
#include<iostream.h>
using namespace std;
int main()
{
int n,temp,digit;
cout<<"Enter the number: ";
cin>>n;
for(digit=0;digit<=9;++digit)   //traverse the digits from 0 to 9
      for(temp=n;temp>0;temp/=10)  //temp/=10 means temp=temp/10
                  if(temp%10 = = digit)
                        cout<<digit;
return 0;
}

No comments:

Post a Comment