C Program that Takes Input as 2323 and Gives Output as 2332

«
»

This is a C program to find a greater number of entered number using same digits.

Problem Description

This program takes a number as input and finds a greater number of the entered number using same digits.

Problem Solution

1. Take a number as input.
2. Reverse the number and store it in the array.
3. Using for loop check if the array[i] is greater than array[i+1]. If it is, then interchange them.
4. The number in the array after swapping is the required number.

advertisement
Program/Source Code

Here is a source code of the C program to find a greater number of entered number using same digits. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program that takes input as 2323 and gives output as 2332. 
  3.  * ie.the new number should be greater than the previous number
  4.  * but should have the same digits
  5.  */
  6. #include <stdio.h>
  7. #include <math.h>
  8.  
  9. int evaluate(int [], int);
  10. int find(int);
  11.  
  12. int main()
  13. {
  14.     int num, result;
  15.  
  16.     printf("Enter a number: ");
  17.     scanf("%d", &num);
  18.     result = find(num);
  19.     if (result)
  20.     {
  21.         printf("The number greater than %d and made of same digits is %d.\n", num, result);
  22.     }
  23.     else
  24.     {
  25.         printf("No higher value possible. Either all numbers are same or the digits of the numbers entered are in decreasing order.\n");
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
  31. int find(int num)
  32. {
  33.     int digit[20];
  34.     int i = 0, len = 0, n, temp;
  35.  
  36.     n = num;
  37.     while (n != 0)
  38.     {
  39.         digit[i] = n % 10;
  40.         n = n / 10;
  41.         i++;
  42.     }
  43.     len = i;
  44.     for (i = 0; i < len - 1; i++)
  45.     {
  46.         if (digit[i] > digit[i + 1])
  47.         {
  48.             temp = digit[i];
  49.             digit[i] = digit[i + 1];
  50.             digit[i + 1] = temp;
  51.  
  52.             return (evaluate(digit, len));
  53.         }
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59. int evaluate(int digit[], int len)
  60. {
  61.     int i, num = 0;
  62.  
  63.     for (i = 0; i < len; i++)
  64.     {
  65.         num += digit[i] * pow(10, i);
  66.     }
  67.  
  68.     return num;
  69. }
Program Explanation

1. Take a number as input and store it in the variable num.
2. Use the functions evaluate() and find() to find the number.
3. In the function find() reverse the input number and store it in the array digit[]. Use while loop to do this.
4. Using for loop check if digit[i] is greater than digit[i+1]. If it is, then swap their values and call the function evaluate().
5. In this function multiply the array elements with the power of 10 and add all the multiplied values to get the required number.

advertisement
Runtime Test Cases
Enter a number: 56732   
The number greater than 56732 and made of same digits is 57632.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn