The Artima Developer Community
Sponsored Link

Java Answers Forum
java looping

2 replies on 1 page. Most recent reply: Dec 11, 2002 6:16 AM by ralph malph

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
ralph malph

Posts: 5
Nickname: ollie
Registered: Dec, 2002

java looping Posted: Dec 11, 2002 2:17 AM
Reply to this message Reply
Advertisement
i am having difficulty creating a triangular number java program, using a loop system.

here is my code:

public class TriNumber1
{
/*Triangular number calculator
Author:Oliver Curtis
Version:1.0
Date:11/11/02 */

public static void main(String[] args)
{
double triNumber, Tn, nth, Int, number; //define variables

//declare and initialise variables
intvalue=1, valueNo= 0, numValue, smallest, n=value, bigger, biggest=-2147483647;

//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.

cheers again for any help.


Sonny

Posts: 9
Nickname: proban
Registered: Dec, 2002

Re: java looping Posted: Dec 11, 2002 4:24 AM
Reply to this message Reply
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);

}

private static int triangleNum(int number)
{
return number*(number + 1)/2;
}
}

ralph malph

Posts: 5
Nickname: ollie
Registered: Dec, 2002

Re: java looping Posted: Dec 11, 2002 6:16 AM
Reply to this message Reply
thanks for the advice, it is greatly appreciated. merry christmas.

Flat View: This topic has 2 replies on 1 page
Topic: Recursive method Previous Topic   Next Topic Topic: Sexy Brunette

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use