the goal of the code is to show the number that if we combine its numebrs it will be the biggest between our range, so if the numbers are 1050 - 950 the number that will show will be 999 because its numbers combined are the biggest between that group of numbers.
For some reason when I run the program and enter the two first numbers (right and left) it shows nothing, also it doesn't show the lines it's always showing when the code is done working.
int main()
{
int right, left, units, tens, hundreds, thousands, save, save1 = 0, num;
printf("Enter big number\n");
scanf_s("%d", &right);
printf("Enter small number\n");
scanf_s("%d", &left);
for (int i = left; i < right; i )
{
num = i;
units = num % 10;
num = num - units;
tens = num %100/10;
num = num - tens;
hundreds = num % 1000 /100;
num = num - hundreds;
thousands = num % 10000/1000;
save = units tens hundreds thousands;
if (save > save1)
{
save1 = save;
}
}
printf("%d\n", save1);
return 0;
}
CodePudding user response:
You are changing the i
value in the for loop and essentially creating an infinite loop make a copy of i
and use that instead in your loop.
for (int i = left; i <= right; i )
{
int n = i;
units = n % 10;
n = n - units;
tens = n % 100;
n = n - tens;
hundreds = n % 1000;
n = n - hundreds;
thousands = n % 10000;
save = units tens hundreds thousands;
if (save > save1)
{
save1 = save;
}
}
CodePudding user response:
There is some changes what you need on your code in order to do what you expect.
First is about the condition on your loop, but i see you already edited that.
Second, you can't change your iterator on the math you are doing every loop and expect to work, so i suggest to use an auxiliary variable.
Third, you need to divide teens per 10 , hundreds per 100 and thousands per 1000 in order to achieve the result you want.
Last but not least you are saving the sum but not the number so when actual sum is bigger than the previous one you should save the number too.
Try to do that modifications by yourself before you see the code i will display here.
This is my resolution to your problem.
Output:
Enter big number
1050
Enter small number
950
999
#include <stdio.h>
int main()
{
int right, left, units, tens, hundreds, thousands, save, save1 = 0, bigSum = 0, aux;
printf("Enter big number\n");
scanf("%d", &right);
printf("Enter small number\n");
scanf("%d", &left);
for (int i = left; i < right; i )
{
aux = i;
units = i % 10;
aux = aux - units;
tens = aux %100;
aux = aux - tens;
hundreds = aux % 1000;
aux = aux - hundreds;
thousands = aux % 10000;
tens /= 10;
hundreds /= 100;
thousands /= 1000;
save = units tens hundreds thousands;
if (save > save1)
{
save1 = save;
bigSum = units tens * 10 hundreds * 100 thousands * 1000;
//or bigSum = i;
}
}
printf("%d\n", bigSum);
return 0;
}
CodePudding user response:
There are several issues in your code. A possibly incomplete list is:
- The extraction of digits is incorrect
- The code saves only the maximal sum of digits, not the number resulting in that sum
- The code allows for negative numbers, but they are not handled and the purpose does not seem to allow negative numbers
- The code in the loop changes the iteration variable
i
causing the loop to malfunction - Digits above thousands (ten-thousands etc.) are not handled
A fixed version could look like this:
#include <stdio.h>
int main()
{
unsigned right, left, n, nMaxSum = 0, sum, maxSum = 0;
printf("Enter big number\n");
scanf_s("%u", &right);
printf("Enter small number\n");
scanf_s("%u", &left);
for (unsigned i = left; i < right; i )
{
sum = 0;
n = i;
while(n != 0)
{
sum = n % 10;
n /= 10;
}
if (sum > maxSum)
{
maxSum = sum;
nMaxSum = i;
}
}
printf("%u (sum: %u)\n", nMaxSum, maxSum);
return 0;
}