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<stdio.h>
int main()
{
int n,temp,digit;
printf("Enter the number: ");
scanf("%d",&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)
                        printf("%d",digit);
return 0;
}

No comments:

Post a Comment