1. ## need help with task

The teacher gave me an assignment to get 10 numbers in the program. Check the sum of digits of a number, and the length of its digits. Check the number about the largest digit length, check the number with the largest number of digits. And if the number with the largest sum of digits of a number is equal to the number with the largest digit length, display in different message. The file did not run well for me, I would be happy for help.
Code:
```#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int len(int n);
int sum(int a);

int main()
{
int x, i = 1, maxcount, maxsum;
scanf("%d", &x);

maxcount = x;
maxsum = x;

while (i <= 9) {
scanf("%d", &x);

if (len(maxcount)<len(x))
maxcount = x;

if (sum(x) > sum(maxsum))
maxsum = x;

i++;
}

if (maxcount==maxsum) {
printf("The sum and the count of the length are equal and the number is: %d \n",maxcount);
}
else {
printf("The number with max length is: %d \n", maxcount);
printf("The number with max sum is: %d \n", maxsum);
}
return 0;
}

int len(int n) {
int i, count = 1;
for (i = 0; i <= n; i++) {
count++;

n /= 10;

}
return count;
}

int sum(int a) {
int i, count = 0;
for (i = 0; i <= a; i++) {
count += (a % 10);

a /= 10;

}
return count;
}```

2. Originally Posted by Afik
Check the sum of digits of a number, and the length of its digits. Check the number about the largest digit length, check the number with the largest number of digits. And if the number with the largest sum of digits of a number is equal to the number with the largest digit length, display in different message.
That's a little confusing to me: what's the difference between "largest digit length" and "largest number of digits"? I'm guessing that you meant:
• Compute the sum of digits of each number and record the number with the largest sum of digits.
• Count the number of digits of each number and record the number with the largest number of digits.
• If the number with the largest sum of digits is equal to the number with the largest number of digits, print the number and a message informing the user of this fact, otherwise print the number with the largest sum of digits and the number with the largest number of digits.

Is this what you meant?

If so, one thing I'd suggest is that instead of always computing len(maxcount) and sum(maxsum), you use these as caches of the largest known. Instead, store the numbers corresponding to the largest len and sum in another pair of variables.

Originally Posted by Afik
The file did not run well for me, I would be happy for help.
What does "did not run well for me" mean? For example, you should provide your test input, expected output, and actual output. It might also good to explain why you think your expected output is correct whereas the actual output is wrong.

Additionally, one of the things to do is to check that your len and sum functions are working as expected before you use them.

3. Originally Posted by laserlight
That's a little confusing to me: what's the difference between "largest digit length" and "largest number of digits"? I'm guessing that you meant:
Originally Posted by laserlight
• Compute the sum of digits of each number and record the number with the largest sum of digits.
• Count the number of digits of each number and record the number with the largest number of digits.
• If the number with the largest sum of digits is equal to the number with the largest number of digits, print the number and a message informing the user of this fact, otherwise print the number with the largest sum of digits and the number with the largest number of digits.

Is this what you meant?

If so, one thing I'd suggest is that instead of always computing len(maxcount) and sum(maxsum), you use these as caches of the largest known. Instead, store the numbers corresponding to the largest len and sum in another pair of variables.

What does "did not run well for me" mean? For example, you should provide your test input, expected output, and actual output. It might also good to explain why you think your expected output is correct whereas the actual output is wrong.

Additionally, one of the things to do is to check that your len and sum functions are working as expected before you use them.

I meant that the sum of digits of the number for example 1234, is 10, and the length of its digits is 4.

And I want to check 10 numbers, and finally if the number with the largest length is also the number with the largest sum of the digits, display in a different message.
The number that both max length and sum length is the max is: %d

and If not, display in 2 messages:
one:
The number with the max length is: %d
two:
The number with the max sum is: %d

The functions work great.
The problem is probably in the int main....

4. Originally Posted by Afik
I meant that the sum of digits of the number for example 1234, is 10, and the length of its digits is 4.

(...)

The functions work great.
Ah, how interesting. I compiled and ran this program:
Code:
```#include <stdio.h>

int len(int n) {
int i, count = 1;
for (i = 0; i <= n; i++) {
count++;

n /= 10;
}
return count;
}

int sum(int a) {
int i, count = 0;
for (i = 0; i <= a; i++) {
count += (a % 10);

a /= 10;
}
return count;
}

int main(void) {
int n = 1234;
printf("Number of digits of %d is %d\n", n, len(n));
printf("Sum of digits of %d is %d\n", n, sum(n));
return 0;
}```
The output that I received was:
Code:
```Number of digits of 1234 is 4
Sum of digits of 1234 is 9```
This contradicts your claim that the functions work great since the sum of digits is incorrect. Then I changed n to 0, and the output I received was:
Code:
```Number of digits of 0 is 2
Sum of digits of 0 is 0```
This contradicts your claim that the functions work great since the number of digits is incorrect.

EDIT:
Originally Posted by Afik
The problem is probably in the int main....
Actually, I fixed len and sub and tested with your main function. While there's room for improvement, the revised program with your main function works great.

5. Originally Posted by laserlight
Ah, how interesting. I compiled and ran this program:
Code:
```#include <stdio.h>

int len(int n) {
int i, count = 1;
for (i = 0; i <= n; i++) {
count++;

n /= 10;
}
return count;
}

int sum(int a) {
int i, count = 0;
for (i = 0; i <= a; i++) {
count += (a % 10);

a /= 10;
}
return count;
}

int main(void) {
int n = 1234;
printf("Number of digits of %d is %d\n", n, len(n));
printf("Sum of digits of %d is %d\n", n, sum(n));
return 0;
}```
The output that I received was:
Code:
```Number of digits of 1234 is 4
Sum of digits of 1234 is 9```
This contradicts your claim that the functions work great since the sum of digits is incorrect. Then I changed n to 0, and the output I received was:
Code:
```Number of digits of 0 is 2
Sum of digits of 0 is 0```
This contradicts your claim that the functions work great since the number of digits is incorrect.

EDIT:

Actually, I fixed len and sub and tested with your main function. While there's room for improvement, the revised program with your main function works great.
ok, can you please show me how to fix it?

6. I fix the functions and now its works!!!
but the int main still with problem...

7. I'd suggest using a while loop instead of a for loop and comparing with 0 in the loop condition.

8. This line in sum() is suspect:

for(i = 0; i <= a; i++)

Also, when it doesn't seem to work for negative numbers:

\$ ./sum_of_digits
Number of digits of -1234 is 1
Sum of digits of -1234 is -10

9. It's good now. but still have a problem in the int main.

Code:
```int sum(int a)
{
int s = 0;
while (a > 0)
{
s += (a % 10);
a /= 10;
}
return s;
}
int len(int n)
{
int c = 0;
while (n > 0)
{
n /= 10;
c++;
}
return c;
}```

10. Originally Posted by Afik
It's good now.
Not quite: len(0) is incorrect, and if you don't intend to handle negative numbers, you should deal with unsigned int rather than int.

Originally Posted by Afik
but still have a problem in the int main.
How does it not work? Just saying that you still have a problem is way too vague. Tell us what you tried (the test input), what you think the result should be (the expected output), and what are the symptoms of the problem (the actual output and why it is wrong).

11. Changing these to work with negative numbers is very easy, just one little change

Look at this line here...
Code:
`while (n > 0)`
And then read out what it is doing

"Do the following while n is larger than 0", which would never work for a negitive number, because they are always going to be smaller than 0.

What you want is to loop while n is not equal to 0.

Easy!

The usual thing to do is to put a special case at the top of the function
Code:
```int len(int n)
{
if(n == [a value that won't work in your algorithm, usually 0])
{
return [something specific to this case];
}

....```
If the user enters the number 0 10 times, do you want the largest length to be 1? Or is this another special case that this number has a length of 0?

If that is the case, don't call len if the user enters 0 (simple if statement should do it)

12. Originally Posted by Click_here
Look at it this way: 10 has a length of 2, but if you divide it by 10, you get 0
You get 1 from that division, not 0. Assuming non-negative input, the issue is specifically for n == 0 because the loop condition is never satisfied, hence c remains 0.

Originally Posted by Click_here
The usual thing to do is to put a special case at the top of the function
That's what I would suggest too, although strictly speaking you can rewrite the loop condition so that you don't need to treat 0 as a special case, and hence set the initial value of c to 1.

13. You get 1 from that division, not 0. Assuming non-negative input, the issue is specifically for n == 0 because the loop condition is never satisfied, hence c remains 0.
Thanks Laserlight, I'll snip that section out of my reply as it might lead to confusion to anyone reading it

14. Calculating the length of a number is one of the rare times where "do while" loops are useful, as it makes one pass of the loop body before the test.

Code:
```int len(int n)
{
int c = 0;

do {
n /= 10;
c++;
} while (n != 0);

return c;
}```
"do while" loops aren't used very often, but sometimes they just make the code better.

15. this what i can't figure it out.
why it say that in length of digits the number is 4900, and not both in sum and in length is 7963