1

I want to count int index = 1;. I want it to be added up on each other. For example: 1 + 2 + 3 etc. How can i do this? I've already tried this:

public void countOneToTen() {
        int index = 1;
        while(index <= 10) {
            index = index + index;
        }
        System.out.println(index);
    }
itvba
  • 235
  • 9
  • 19

3 Answers3

4

You need to decouple your "sum" variable and your index variable.

Introduce another variable to hold the sum:

public void countOneToTen() {
    int sum = 0;
    int index = 1;
    while (index <= 10) {
        sum += index;
        index++;
    }
    System.out.println(sum);
}

Also, use for loops to limit variable scope:

public void countOneToTen() {
    int sum = 0;
    for (int index = 1; index <= 10; index++) {
        sum += index;
    }
    System.out.println(sum);
}

Your approach didn't work because your index should go from 1 to 10, but the moment you modify it you throw this iteration off course.

Here's what I mean:

index =                 1
// index: 1
index = index + index = 2
// index: 2
index = index + index = 4
// We've skipped 3.
// etc.
index = index + index = 8
index = index + index = 16 > 10

1 + 2 + 4 + 8 = 15

Just for fun, here's the Java 8 stream approach:

IntStream.rangeClosed(1, 10).sum();

(thanks to Oleg for pointing out the existence of rangeClosed)

:)

and here's the single-statement approach using the general summation of an arithmetic sequence:

image

int sum = terms * (min + max) / 2;
Salem
  • 13,516
  • 4
  • 51
  • 70
2

Introduce a new variable to store your current sum.

public void countOneToTen() {
        int index = 1;
        int sum = 0;
        while(index <= 10) {
            sum += index;
            ++index;
        }
        System.out.println(sum);
    }

or you could use Gauss' trick. let n = 10 then the sum from 1 to n is n*(n+1)/2

Oleg
  • 1,479
  • 3
  • 21
  • 42
0
int sum = 0;

    for (int i = 0; i <= 10; ++i) {
        sum += i;
        System.out.println(sum);
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Michael
  • 120
  • 9
  • This didn't work. We get the answer 15 in stead of 55. – itvba Dec 09 '17 at 21:00
  • You need an extra `sum` variable – Michael Dec 09 '17 at 21:02
  • 3
    What is Stackoverflow for then? I'm stuck with a question that i need answered. I thought Stackoverflow is a website where you can ask questions. I'm not using Stackoverflow to make my homework, i'm using it to get better in programming. Also, i want to give a clearer question but programming for beginners can be a bit confusing. You've all been there, right? – itvba Dec 09 '17 at 21:06
  • 'I'm stuck with a question that i need answered' Basically 10 seconds into googling and you find the answer. Think for yourself before you ask others for help. – Michael Dec 09 '17 at 21:09
  • 1
    Think for yourself before asking others for help? I've used Google and i couldn't find the answer because i'm new to programming and finding problems can be a bit difficult because i don't know how to describe my programming problem. I'm sorry for using Stackoverflow. – itvba Dec 09 '17 at 21:10
  • 3
    @Michael The community isn't anti-homework questions, it's just anti-low-quality questions. I think everyone here should read this: [How do I ask and answer homework questions?](//meta.stackoverflow.com/a/334823). I don't see anything currently wrong with the question. – 4castle Dec 09 '17 at 21:10
  • 3
    The question fulfills the basic qualities of a question - it _shows an attempt_, shows what is wrong and has a clear purpose. – Salem Dec 09 '17 at 21:11
  • You can't just say: Yeah I am new to programming so let's go to an platform and ask every little question I have. Did you ever consider some self study? Find a book, get to know the basics. – Michael Dec 09 '17 at 21:12
  • I have the book: Objects first with BlueJ in Java sixth edition. The question was asked there. Anyway, sorry for asking lol – itvba Dec 09 '17 at 21:15
  • 1
    The Internet is most definitely a valid form of self-study, and as long as [there is a clear attempt](http://idownvotedbecau.se/noattempt/) I see no problem with this. The question was properly formulated. Now, [THIS](https://stackoverflow.com/questions/20057935/what-is-system-currenttimemillis) on the other hand... – Salem Dec 09 '17 at 21:15
  • 3
    If you didn't think the question was clear, and want to avoid the debate, probably should not have been so fast to answer – OneCricketeer Dec 09 '17 at 21:17