# Help With Simple Summation...

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-06-2006
TOPFLOR
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; }```
• 08-06-2006
zx-1
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
• 08-06-2006
quzah
(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.
• 08-06-2006
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.
• 08-06-2006
quzah
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.
• 08-07-2006
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.
• 08-07-2006
quzah
Well if you're going to half-ass it, that might do the trick. But what happens when I enter three digits?

Quzah.
• 08-07-2006
venu
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.
• 08-07-2006
whiteflags
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.
• 08-07-2006
quzah
point
-----

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.
• 08-08-2006
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);

}
• 08-08-2006
quzah
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.
• 08-08-2006
cdalten
And how do you propose we handle situations were a person wants to sum up say like:
12 and 15? Your solution breaks down.
• 08-08-2006
quzah
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.
• 08-08-2006
cdalten
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last