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. :(sweat):
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.
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.
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!!!
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 );
}
}
Re: For Loop Help! Completely Lost
Quote:
NO, your going in the wrong direction. You already have the triangle number
But the OP's question said "Write and test a function
int triangular(int n)
which returns the nth triangular number".
That is, the triangular number has to be calculated.
Quote:
I found the formulae here:
Calculate a triangle number
And it's a good formula, but "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".
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.
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:
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 );
}
}
Re: For Loop Help! Completely Lost
Quote:
declare all variables that give you an error, such that all blocks can access them.
As, I think, had already been said.