This is a C program to find a greater number of entered number using same digits.
This program takes a number as input and finds a greater number of the entered number using same digits.
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.
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.
/*
* C program that takes input as 2323 and gives output as 2332.
* ie.the new number should be greater than the previous number
* but should have the same digits
*/
#include <stdio.h>
#include <math.h>
int evaluate(int [], int);
int find(int);
int main()
{
int num, result;
printf("Enter a number: ");
scanf("%d", &num);
result = find(num);
if (result)
{
printf("The number greater than %d and made of same digits is %d.\n", num, result);
}
else
{
printf("No higher value possible. Either all numbers are same or the digits of the numbers entered are in decreasing order.\n");
}
return 0;
}
int find(int num)
{
int digit[20];
int i = 0, len = 0, n, temp;
n = num;
while (n != 0)
{
digit[i] = n % 10;
n = n / 10;
i++;
}
len = i;
for (i = 0; i < len - 1; i++)
{
if (digit[i] > digit[i + 1])
{
temp = digit[i];
digit[i] = digit[i + 1];
digit[i + 1] = temp;
return (evaluate(digit, len));
}
}
return 0;
}
int evaluate(int digit[], int len)
{
int i, num = 0;
for (i = 0; i < len; i++)
{
num += digit[i] * pow(10, i);
}
return num;
}
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.
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 Books in C Programming, Data-Structures and Algorithms
- Check Computer Science Books
- Apply for C Internship
- Practice BCA MCQs
- Practice Computer Science MCQs
- Check C Books