Results 1 to 9 of 9
Thread: For Loop Help! Completely Lost
 04172012, 01:34 AM #1Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
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.
Java Code:public static void triangularNumber() { Scanner input = new Scanner(System.in); System.out.print("Enter number: "); int x = input.nextInt(); int triangularNumber = (triangular(x)); System.out.println("The" + x + "triangular number is" + addition); } public static int triangular(int n) { for (int total = 0; total < triangularNumber + 1;total++) { int addition = addition + total; } return addition; }

Re: For Loop Help! Completely Lost
Please tell us your errors as they often are very helpful by telling you what you're doing wrong.
 04172012, 02:02 AM #3Member
 Join Date
 Feb 2012
 Posts
 8
 Rep Power
 0
Re: For Loop Help! Completely Lost
The compiler can't find variable addition and triangularNumber. The variable addition can't be initialized.

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.
 04172012, 07:36 AM #5Member
 Join Date
 Apr 2012
 Location
 Spainish TOwn
 Posts
 25
 Rep Power
 0
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.
I found the formulae here:
Calculate a triangle number
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!!!
PHP Code:public class triangleNumber_NthFinder { public static void main ( String [ ] args ) { int nth = 0, triangle_number = 0; java.util.Scanner scanner = new java.util.Scanner ( System.in ); //get user input System.out.print ( "Enter a triangle number : " ); triangle_number = scanner.nextInt ( ); //compute nth value nth = ( int ) Math.sqrt ( 2 * triangle_number ); //show result System.out.print ( "result : " + nth ); } }
Last edited by Bushman; 04172012 at 07:40 AM. Reason: forgot to add important note
 04172012, 07:53 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
Re: For Loop Help! Completely Lost
NO, your going in the wrong direction. You already have the triangle number
int triangular(int n)
which returns the nth triangular number".
That is, the triangular number has to be calculated.
I found the formulae here:
Calculate a triangle number
 04172012, 08:20 AM #7Member
 Join Date
 Apr 2012
 Location
 Spainish TOwn
 Posts
 25
 Rep Power
 0
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.
 04172012, 08:37 AM #8Member
 Join Date
 Apr 2012
 Location
 Spainish TOwn
 Posts
 25
 Rep Power
 0
Re: For Loop Help! Completely Lost
Anyway, you just need to declare all variables that give you an error, such that all blocks can access them.
HEre is a quickly written solution for you:
PHP Code:public class trinagleNumberFinder { public static void main(String[] args) { int summation = 0, nth =0; java.util.Scanner scanner = new java.util.Scanner ( System.in ); System.out.print ( "Enter nth number : " ); nth = scanner.nextInt ( ); for (int i = 0; i < nth; i++) summation += ( i + 1 ); System.out.println ( "triangle number(summation 1 to nth) : " + summation ); } }
 04172012, 11:01 AM #9Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,716
 Rep Power
 18
Similar Threads

Help plz: not completely working JAR
By linkingabo in forum NetBeansReplies: 6Last Post: 01282012, 04:32 AM 
HELP!!! Completely confused on how to fix this program!!!
By BeginnerJava in forum New To JavaReplies: 17Last Post: 06122011, 11:59 PM 
completely new
By battosai16 in forum New To JavaReplies: 1Last Post: 08052010, 07:43 PM 
Completely new to Java problem
By Seamo14 in forum New To JavaReplies: 4Last Post: 10092008, 12:50 PM 
I am completely stuck
By jpnym15 in forum New To JavaReplies: 2Last Post: 11142007, 06:40 PM
Bookmarks