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. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    42

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    is it possible for me to do it in while loop?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes, I think you're supposed to use loop.
    and yes we won't do it for you.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure. You could also do it with sprintf. There are lots of ways to do it.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    What do you think the difference is between this:
    Code:
    (number%10000)%1000%100%10
    and just this?:
    Code:
    number % 10
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    (number%10000)%1000%100%10
    this code is to find the last remainder or last digit of the number.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    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 subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21