0
C program that returns the next largest number that can be created from the same digits as the input.Example:58943 gives 59348
C program
5 Respostas
+ 2
Might you show us your try?
We won't solve your homework here but if you show your effort we will be happy to help.
+ 1
Vamsidhar Reddy
what errors you find?
Only 1 warning, to remove,
Just include math header..
#include<math.h>
Your program working perfectly...
fine for me with test cases..
You can link code by saving, if you want, it'll help rectify mistakes easily.. See the following link on how to share links 👇
https://www.sololearn.com/post/74857/?ref=app
0
Yes. Show your try...
is this a challenge question, or homework?
Description to get answer:
Form array by individual digits first, then
Find largest index i such that array[i− 1] < array[i].
(If no such i exists, then this is already the last permutation.)
Find largest index j such that j ≥ i and array[j] > array[i − 1].
Swap array[j] and array[i − 1].
Reverse the suffix starting at array[i].
See this link for an algorithm
https://www.nayuki.io/page/next-lexicographical-permutation-algorithm
next lexicographic permutation algorithm
0
#include <stdio.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;
}
0
Still there is a small mistake try to find out