Find maximum number formed using digits of a number in C++

In this tutorial, we will learn how to find the maximum number that can be formed using digits of a number in C++. So, we will manipulate the digits of the given number and find out the maximum number that we can form using those digits. Here, you will get to know the logic to generate the maximum number out of some digits, and a C++ program for implementation of that logic.

How to obtain digits of a number in C++

Firstly, we have to get the individual digits from the given number. So, to get the digits, follow the following steps –

  1. Take an array to store the digits.
  2. Perform modulus operation on the number by 10 and store the result in the array.
  3. Increment the array index counter by 1.
  4. Divide the number by 10 and store the result in the variable which contains this number.
  5. Repeat from step 2 again until the value of the number becomes 0.
  6. Stop, the digits are stored in the array.

The example below explains the working of the above steps.

Number = 450
Array[]
i = 0

--Iteration 1--
Condition : Number = 450 > 0 (true)
Array[i] = Number % 10
i = i + 1
Number = Number / 10
Array contains : {0}

--Iteration 2--
Condition : Number = 45 > 0 (true)
Array[i] = Number % 10
i = i + 1
Number = Number / 10
Array contains : {0,5}

--Iteration 3--
Condition : Number = 4 > 0 (true)
Array[i] = Number % 10
i = i + 1
Number = Number / 10
Array contains : {0,5,4}

--Iteration 4--
Condition : Number = 0 > 0 (false)
Stop
Array contains : {0,5,4}

Logic to get the maximum number from digits

Now, our task is to arrange the array containing individual digits in such a way that it generates the maximum number out of those digits. So, we can apply any sorting algorithms to the array to sort the digits in descending order. Let us take the above example in which the array contents are –

Array[] = { 0 , 5 , 4 }

If we sort this array in descending order using any sorting technique, we get the following array –

Array[] = { 5 , 4 , 0 }

So, after combining these digits we can form the maximum number i.e. 540 in this case.

C++ program to generate the maximum number from digits of a given number

Now, we will see a program that generates the maximum number from digits of the number taken from the user as an input. To do so, we have to perform three tasks –

  • Firstly, Obtain the individual digits from the number.
  • Secondly, sort the array containing individual digits in descending order.
  • Finally, combine the digits in the sorted array to get the maximum number.

So, the C++ program is –

#include<iostream>
#define MAX_DIGIT 10
using namespace std;
int main()
{
  long num,max_num;
  int d_array[MAX_DIGIT],digit,count=0,i,j,temp;
  cout<<"\nENTER A NUMBER : ";
  cin>>num;
  while(num!=0)
  {
    digit=num%10;
    num=num/10;
    d_array[count]=digit;
    count++;
  }
  for(i=0;i<count;++i)
  {
    for(j=i+1;j<count;++j)
    {
      if(d_array[i]<d_array[j])
      {
        temp=d_array[i];
        d_array[i]=d_array[j];
        d_array[j]=temp;
      }
    }
  }

  j=1;
  max_num=0;
  for(i=count-1;i>=0;--i)
  {
    max_num+=d_array[i]*j;
    j*=10;
  }
  cout<<"MAXIMUM NUMBER THAT CAN BE FORMED IS : "<<max_num;
}

To combine the digits of the sorted array to form the maximum number, traverse the array in reverse order and multiply each digit ‘j’ number of times by 10. The value ‘j’ starts from 1 and is incremented by 1 in each iteration.

C++ program output

The program displays the maximum number that can be formed from the digits of the number entered by the user. The output of the above program is –

[email protected]:~/cpp$ g++ max.cpp
[email protected]:~/cpp$ ./a.out

ENTER A NUMBER : 1195728
MAXIMUM NUMBER THAT CAN BE FORMED IS : 9875211
[email protected]:~/cpp$

Thank you for reading this tutorial. I hope it helps you.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *