Big problems with assignment, please help!!
Write a program called sumk.c which prints the sum of the numbers from 1 to k, for all values of k between 1 and 20.
Your program should print a table in this format:
k sum
--------
1 1
2 3
3 6
4 10
5 15
6 21
The code I wrote, is just a simple one to start off with:
Code:
#include <stdio.h>
int main (int argc, char **argv) {
int sum; //Final sum
int k;
printf ("Please enter integer number which is at least 1 or at most 20 : \n");
scanf ("%d", &k);
while (k <= 20 ){
sum = 0;
k = k + 1;
sum = sum + k;
}
printf ("The sum is : %d \n", sum);
return (0);
}
It keeps giving me a value for the sum as 21, no matter what I type!!
I removed sum = 0; and it gives me 232 when i enter 1, and 230 when i enter 2 and 227 when i enter 3 and so on in that pattern...
Which is kind of right, because it should actually be INCREASING in that pattern, +1, +2, +3. But it should start at 1, so it goes, 1, 3, 6. etc.
I have no idea why it is giving me such large values..
Thanks