Shankar Kumar
Shankar Kumar

Reputation: 2357

Simple program that adds numbers from user

I am trying to write a simple program that uses scanf to input 5 numbers from the user and add them together. Here is the code I have so far..

  int main()   
    {

    int i;  
    int j=1;  
    int k=1;

            for (i=1; i<= 5; i++)
            {
                  scanf("%d\n", &j);
                  k = k+j;
            }   
            printf("%d\n", k);
    }

But here's what happens when I run the program:

1  
2  
3  
4  
5  
5  
16

Basically, it asks me for a sixth number (obviously, I just need 5), and it also adds one to the final result. (1+2+3+4+5=15).

Any thoughts on this. Am I making a simple mistake somewhere?

Upvotes: 0

Views: 702

Answers (5)

spatara
spatara

Reputation: 901

The '\n' character is unnecessary. I suspect you are mixing up your printf and scanf syntax :P

Upvotes: 0

Anonymous
Anonymous

Reputation: 572

This is what you want, k initialized at 0 and doing a scanf input without the \n which is an endline :

int main() {
    int i;  
    int j=0;  
    int k=0;
    for (i=1; i<= 5; i++){
        scanf("%d", &j);
        k = k+j;
    }   
    printf("%d\n", k);
}

Upvotes: 0

Marshall Conover
Marshall Conover

Reputation: 845

You seem to be initializing k (which is the number you hold your sum in) as one, then adding all the other numbers to it. Try this:

int k = 0;

instead.

Then, when you do k = k+j

the first time, k will be 0, and not 1. You also don't need to do j=1.

That said, you can also use a shortcut for k = k +j;

k += j;

C programmers have to do this pattern so much that they built a shortcut into the language specifically for it.

In your for loop, it's convention in C to start at zero and work to < your max number, as well:

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

I'm not sure why it's asking an extra time, but try setting your loop as that and seeing if it works.

Upvotes: 1

Roland Illig
Roland Illig

Reputation: 41627

Initially k = 1. Then you add the numbers 1, 2, 3, 4, 5 to it. Altogether they sum up to 1+1+2+3+4+5, which is 16.

You should generally think about initializing variables.

  • i doesn't need to be initialized before the for loop.
  • j doesn't need to be initialized, since it will be read from the input.
  • k needs to be properly initialized. But since it has a certain purpose you should rather call it sum than k. And when you sum up things, you should start with 0.

Additionally you should check whether the call to scanf was successful. In that case the function returns 1.

if (scanf("%d", &j) == 1) {
  sum += j;
} else {
  fprintf(stderr, "Invalid input.\n");
  break; /* exit the for loop. */
}

Upvotes: 1

VeeArr
VeeArr

Reputation: 6178

As others have said, you are initializing k incorrectly, but I suspect that what's causing your problem is that you are using scanf("%d\n", &j); instead of scanf("%d", &j);. scanf() ignores whitespace leading up to the match.

Upvotes: 1

Related Questions