Help With Simple Summation...

This is a discussion on Help With Simple Summation... within the C Programming forums, part of the General Programming Boards category; i need to write a program instructing users to enter 5 digit integer. and the program must add each integer ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    Help With Simple Summation...

    i need to write a program instructing users to enter 5 digit integer. and the program must add each integer and sum it for the final result.


    for ex 12345 would yield 1+2+3+4+5= 15


    when i try using scan f as %d, it reads the ENTIRE 5 digit integer as ONE. when i use %c it calculates a crazy sum for the final answer... here is my work thus far.

    Code:
    #include <stdio.h>
    int main ()
    {
    ;
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    printf("enter 5-digit number:");
    scanf(" %d%d%d%d%d",&num1,&num2,&num3,&num4,&num5);
    
    
    sum=num1+num2+num3+num4+num5;
    printf("summing %d+%d+%d+%d+%d= %d\n",num1,num2,num3,num4,num5,sum);
     return 0;
    }
    Last edited by Ken Fitlike; 08-08-2006 at 09:18 AM. Reason: inserted code tags

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by TOPFLOR
    when i try using scan f as %d, it reads the ENTIRE 5 digit integer as ONE.
    Obviously because it is one.

    Either (a) change your design to put spaces between the numbers, or (b) get characters and subtract each by '0' (numeral zero between single quotes).

    And put your code between [code][/code] tags
    Last edited by zx-1; 08-06-2006 at 04:51 PM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    (c) Use 1 number to read the actual number, and one more to sum it.
    Code:
    x = readnumberintox( );
    
    while x
        sum += x mod 10
        x /= 10
    Naturally you will want to take the time to figure out why this works, before you simply turn it in for homework. I'm sure if you wait around, someone will explain it line by line so you don't actually have to learn anything at all.


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

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    I was looking to redeem myself for overcomplication the factorial problem on a previous thread.....

    Nah....I won't post another solution.

    Calm down.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cdalten
    I was looking to redeem myself for overcomplication the factorial problem on a previous thread.....

    Nah....I won't post another solution.

    Calm down.
    Calm down? Who said I was talking about you? Someone's feeling guilty.


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

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Hi,
    If you want to take each number into separate variable, you can go with this.

    main()
    {
    int i,j,k,l,m;
    scanf("%1d%1d%1d%1d%1d",&i,&j,&k,&l,&m);

    printf("%d",i+j+k+l+m);
    }

    give input as 12345
    now it each digit is taken into new variable .
    and the result would be the sum of all five vars(15 here).
    If you give more than 5 digits, it will ignore remaining digits.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well if you're going to half-ass it, that might do the trick. But what happens when I enter three digits?


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

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Quote Originally Posted by venu
    Hi,
    If you want to take each number into separate variable, you can go with this.

    main()
    {
    int i,j,k,l,m;
    scanf("%1d%1d%1d%1d%1d",&i,&j,&k,&l,&m);

    printf("%d",i+j+k+l+m);
    }

    give input as 12345
    now it each digit is taken into new variable .
    and the result would be the sum of all five vars(15 here).
    If you give more than 5 digits, it will ignore remaining digits.
    If you give only 3 digits, then it will ask user for two more digits. It will calculate summation for first five digits entered.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    How do i missed point?

    Quzah was saying that it is better to provide a decent solution rather than focusing on minutae based on how the exercise was worded. That is, with a good solution we should be able to find the sum of other numbers not in the tens of thousands.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    point
    -----
    head

    Since you mentioned input other than the five that they required, I also pointed it out. There's no reason to intentionally limit yourself.


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

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    I feel this can be used for generic
    #include <stdio.h>

    main()
    {
    char a[50] = {0};
    int k,l=0;
    scanf("%s", a);
    k = atoi(a);
    while(k)
    {
    l+=(k%10);
    k/=10;
    }
    printf("\n%d",l);

    }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you reading a string when you just convert it to an integer anyway? Either user a string, thus you can enter numbers like 23871298371098712918, or use an int, or maybe a long long, and use the method I described in the first reply. There's no need for both.

    Also, why are you not using CODE TAGS? Go read the forum guidelines until you figure out how to use code tags. Do not pass GO, do not collect $200.


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

  13. #13
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    And how do you propose we handle situations were a person wants to sum up say like:
    12 and 15? Your solution breaks down.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you talking about? Rather, who are you talking about? That's what the 'quote' button is for. Mine works fine. It even does negatives; I know, I tested it.


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

  15. #15
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by venu
    I feel this can be used for generic
    #include <stdio.h>

    main()
    {
    char a[50] = {0};
    int k,l=0;
    scanf("%s", a);
    k = atoi(a);
    while(k)
    {
    l+=(k%10);
    k/=10;
    }
    printf("\n%d",l);

    }
    Q, I could be a real dick and say ask your local college for a refund, but a demo from my computer is enough.

    $emacs -nw sumit.c
    $gcc -Wall sumit.c -o sumit
    sumit.c:4: warning: return type defaults to `int'
    sumit.c: In function `main':
    sumit.c:8: warning: implicit declaration of function `atoi'
    sumit.c:16: warning: control reaches end of non-void function

    First off the compiler isn't amused by the lines of code. Bad sign.

    I want to sum 1 and 2
    $./sumit
    12

    3$
    This produces the correct result.

    now I try 12 and 15
    $./sumit
    1215

    9$

    The wrong number. Ie, undefined behavior. Maybe you should actually sit down and try to code 10 lines of c code on your own once instead of critizing others. If you feel you are above coding, then please tell us what books, academic papers, or sourceforge projects you have to your name.

    I mention the book thing because there is are two people on comp.lang.c that have the same attitude as you. However, even though they don't post code, they have verifiable books and academic publications to their names.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

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