Computer Science, asked by jegdeshvar1414, 1 month ago

A number N is passed as the input. The program must print the largest possible number with the digits in the given number N. Input Format: The first line contains the value of N. Output Format: The first line contains the largest possible number with the digits in N. Boundary Conditions: 1 ​

Answers

Answered by pruthaasl
0

Answer:

#include <iostream>

using namespace std;

int main() {

   int a[10];

   int n,t,j,i=0,c=0;

   cout<<"\nEnter a number: ";

   cin>>n;

   

   while(n!=0)

   {

       a[i]=n%10;

       n/=10;

       i++;

       c++;

   }

   for(i=0;i<c;i++)

   {

       for(j=0;j<c;j++)

       {

       if(a[i]>a[j])

       {

           t=a[i];

           a[i]=a[j];

           a[j]=t;

       }

       }

   }

   cout<<"\nThe largest possible number is ";

   for(i=0;i<c;i++)

   cout<<a[i];

   return 0;

}

Explanation:

  1. Declare the necessary variables required to store the number and an array to store the digits.
  2. Take the number as input from the user and store it.
  3. Separate the digits of the number using the modulo operator and store them in an array.
  4. Compare the separated digits using the for and if loops and arrange them in descending order.
  5. Print the largest possible number as the output.

#SPJ3

Similar questions