The Artima Developer Community
Sponsored Link

Java Answers Forum
loops

3 replies on 1 page. Most recent reply: Dec 12, 2002 3:49 AM by V. Srikanth

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 3 replies on 1 page
ralph malph

Posts: 5
Nickname: ollie
Registered: Dec, 2002

loops Posted: Dec 9, 2002 12:19 PM
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.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: loops Posted: Dec 9, 2002 12:47 PM
Reply to this message Reply
As a previous reply asked, could you define triangular number?

ralph malph

Posts: 5
Nickname: ollie
Registered: Dec, 2002

Re: loops Posted: Dec 9, 2002 1:00 PM
Reply to this message Reply
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 for ant help

V. Srikanth

Posts: 9
Nickname: iamnobody
Registered: Dec, 2002

Re: loops Posted: Dec 12, 2002 3:49 AM
Reply to this message Reply
Hi Oliver,

I guess, you want to print first 'n' triangle #s..
If that's the case, see if u can use foll'g logic:

hv 3 variables:
int i=0, j=0, k=0, n=10;
for (int j=1; j<=n; j++, i++) {
k = (i * (i+1))/2;
System.out.print(k+" ");
}
System.out.println();

The above code wud print first 10 triangle #s:
1 3 6 10 15 21 28 36 45 55

Thanx,
-Srikanth.

I am nobody. Nobody is perfect. So, I am perfect:)

Flat View: This topic has 3 replies on 1 page
Topic: JDeveloper Previous Topic   Next Topic Topic: Re:well these are some important questions in java, want its answers???

Sponsored Links



Google
  Web Artima.com   

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