Thread: How to make greatest 3 digit number from given number

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    How to make greatest 3 digit number from given number

    please help me i m making program which takes a number from user and then make greates 3 digit number from the digits of that number
    ex. if user type 57149 then output should be 975 because this is the greatest number that can be formed by digits. i tried below code but when i type number nothing is shown

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int a[20],n,no,i,cnt=0;
        printf("Enter a no.");
        scanf("%d",&no);
        n=no;
        while(no!=0)
        {
            no=n/10;
            cnt++;
        }
                for(i=0;i<cnt;i++)
                {
                    a[i]=n%10;
                    n=n/10;
                    if(n==0)
                    break;
                }
        for(i=0;i<cnt;i++)
        printf("%d\n",a[i]);
        return 0;
    }
    above program even not printing the digits of number. so i didnt type further but once i get code to separate digits i will sort the array and will print first 3 numbers because we want 3 digit largest number
    but first someone correct this code so it can separate digits please help me
    thanks

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I think very confusing variable naming convention n and no. It gets struck in while loop i suppose.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A development process

    Step 1 - input a number (say 57149) and output say
    found 5
    found 7
    found 1
    found 4
    found 9

    Step 2 - increment count[x] for each digit found.

    Step 3 - solve the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Salem View Post
    A development process

    Step 1 - input a number (say 57149) and output say
    found 5
    found 7
    found 1
    found 4
    found 9

    Step 2 - increment count[x] for each digit found.

    Step 3 - solve the problem.
    Very good hint at a simple solution.
    I was thinking sorting solution; but, using an count[x] array is must simpler.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-22-2014, 07:00 AM
  2. Convert 8-digit Binary Number to decimal number
    By yongsheng94 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2013, 09:47 AM
  3. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  5. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM

Tags for this Thread