For Loop Help! Completely Lost

1. Write and test a function
int triangular(int n)
which returns the nth triangular number. A triangular number is a number which is the sum of the first n positive integers. So, for example 15 is the 5th triangular number since 15 = 1 + 2 + 3 + 4 + 5. Some check values:
• The 10th triangular number (triangular(10)) is 55.
• The 100th triangular number is 5050
• The 1337th triangular number is 894453.

Note: you must do this problem using a for loop to add the numbers; even if you know a “shortcut” formula to find the sum, don’t use it.
This assignment contains only 3 problems, but you will need to write 7 methods: the main method, plus 2 methods for each problem. For each problem, you will need to write one method that takes one or more arguments and returns a value, but which DOES NOT PRINT ANYTHING (like the isLeapYear method in my MenuDemo file), and another method which handles user input and output, and which calls the other method (like the leapYear method in my MenuDemo file). Make sure to copy the MenuDemo code to your file to act as a structure for your assignment.

This is what I have so far. I got errors. I don't even know if I'm headed the right direction.

Re: For Loop Help! Completely Lost

You're running into what's known as a scope issue in that variables declared inside of a method (or any block for that matter such as an if block or for loop) are only visible inside of the method or block in which they've been declared. So since triangularNumber has been declared within the main method, it is visible only in the main method, and since addition has been declared inside of the for loop, it is only visible inside of the for loop. To fix addition, declare it *before* the for loop, assigning it initially equal to 0. For triangularNumber, perhaps you meant to use the n parameter inside of your method.

Re: For Loop Help! Completely Lost

NO, your going in the wrong direction. You already have the triangle number, so you don't need any form of for loop to do anything.
What you need is to transpose the formulate used to derive that triangle number, making the summator the subject.

NOW,
where triangle number = i * ( i + 1 ) / 2,
We want i, which is the number of positive integers that summate to form the triangle number.
So, we need to isolate this i from the RHS[i * ( i + 1 ) / 2], so we need to make i the subject of
this formulae."
So:
A.(i + 1)/2 = n/i
B.(i + 1)/2 = n/i*2;
C.i= 2n/i
D.2n=i^2
E.i=root(2n) DONE!!!

Re: For Loop Help! Completely Lost

Lol, I've been misreading posts on many forums since all of today. I haven't gotten any sleep.....it wouldn't be a shortcut formulae if the only the nth value was needed, here go a loop wouldn't be needed. But anyhow, the site i linked the OP to should give the exact java code required to do this.