# help to shorten my code.

This is a discussion on help to shorten my code. within the C Programming forums, part of the General Programming Boards category; five-digit number is input through the keyboard, calculate the sum of its digits. i think my code is too long. ...

1. ## help to shorten my code.

five-digit number is input through the keyboard, calculate the sum of its digits. i think my code is too long. can anyone help with other best way to get the same result as mine. thanks alot.

Code:
#include<stdio.h>
int main()
{
int number;
printf("input five-digit number :\n");
scanf("%d",&number);
printf("sum is %d\n",number/10000+(number%10000)/1000+(number%10000)%1000/100+(number%10000)%1000%100/10+(number%10000)%1000%100%10);
}

2. Code:
#include<stdio.h>
int main()
{
int number,N=10000;
printf("input five-digit number :\n");
scanf("%d",&number);
printf("sum is %d\n",number/N+(number%N)/1000+(number%10000)%1000/100+(number%N)%1000%100/10+(number%N)%1000%100%10);
}
Or perhaps:
Code:
#include<stdio.h>
#define N (number%10000)
int main()
{
int number;
printf("input five-digit number :\n");
scanf("%d",&number);
printf("sum is %d\n",number/10000+N/1000+N%1000/100+N%1000%100/10+N%1000%100%10);
}
Quzah.

3. is it possible for me to do it in while loop?

4. Yes, I think you're supposed to use loop.
and yes we won't do it for you.

5. Sure. You could also do it with sprintf. There are lots of ways to do it.

Quzah.

6. What do you think the difference is between this:
Code:
(number%10000)%1000%100%10
and just this?:
Code:
number % 10

7. (number%10000)%1000%100%10
this code is to find the last remainder or last digit of the number.

8. So if there were a series of steps you could take to do what you already know, how would you start? You should to look to see if the dividend and divisor change in a deterministic way.

Popular pages Recent additions