//ask user to input a value for n numValue=WinKBInput.readInt("Enter a value for n");
//loop while number of values not exceeded while (valueNo<numValue) { //ask user for a value for n Tn=WinKBInput.readInt("Enter a value for n");
//if n is smaller than stored biggest if (value>biggest) biggest=value; //update biggest //increment value counter valueNo++; }
//perform calulation triNumber=(n+1);
//output result System.out.println("n+1 equals"+triNumber);
//perform calulation triNumber=(n*(n+1));
//output result System.out.println("n*(n+1) equals"+triNumber);
//perform calculation triNumber=(n*(n+1)/2);
//output result System.out.println("n*(n+1)/2) equals" +triNumber);
//output result System.out.println("The triangular number is" +triNumber); } }
i would appreciate it greatly if you could guide me where i have gone wrong. i am not asking you to do it for me, just some guidance in the right direction would be greatly appreciated.
this is the formula for triangular numbers:
to work out a triangular number you need to use the formula:
Tn=n(n+1)/2
ie. 1. n+1 2.times answer to 1. by n 3.divide the outcome of 2. by 2
ie.n=2 tn=2(2+1)/2 tn=2*3/2 tn=6/2 tn=3
the 2nd triangular number is 2.
n is the variable that the user inputs.
triangular number pattern:1,3,6,10,15,21,28,36,45,55,66,78 etc.
Hi Olier, You should have a separate method to compute the triangle number, pass a number to it, it will return the triangle number. The loop used for user input. You should learn how to get the input from user. Here is a simple working sample for your asignment:
import java.io.*; public class TriangleNumber { public static void main(String [] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int num = 0;
do { System.out.print("Enter number: "); num = Integer.parseInt(in.readLine());
System.out.println("The triangular number is: " + triangleNum(num)); }while(num != 0);