20 March, 2012

C++ Program Printing Digits Of A Number In An Ascending Ordered Form.

This Post Contains A C++ Program Printing Digits Of A Number In An Ascending Ordered Form With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'for-Loops' & 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program For Printing Digits Of A Number In An Ascending Ordered Form.


# include <iostream.h>
# include <conio.h>

void main()
{
                long n, s;
                int    i,    j,    k,    t,    a[ 10 ];
                clrscr();
                cout << " Enter Any Number  :  ";
                cin >> n;
                 j   =   0 ;
                while  (    n    >    0    )
                {
                          i   =   n   %   10;
                         a[ j ]   =   i;
                         j++ ;
                         n   =   n   /   10;
                }
                for   (   i  =  0;   i  <  j;   i++   )
               {
                           for   (   k  =  i+1;    k  <  j;   k++   )
                          {
                                          if   (   a[ i ]   >   a[ k ]   )
                                               t   =   a[ i ] ;
                                               a[ i ]   =   a[ k ] ;
                                               a[ k ]   =   t ;
                           }
                }
                 t   =   j   -   2 ;
                 n   =   s   =   0 ;
                 for   (   i  =  0;   i  <  j;   i++  )
                {
                             s   =   a[ i ] ;
                             for   (   k  =  0;   k  <=  t;   k++   )
                            {
                                         s   =   s   *   10 ;
                                         t-- ;
                                         n   =   n   +   s ;
                            }
                }
                cout << " \n Ordered No.   :   " << n; 
                getch();
}

Output :

Enter Any No.   :   312

Ordered No.   :   123    




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.