The Artima Developer Community
Sponsored Link

Java Answers Forum
triangular numbers tester

2 replies on 1 page. Most recent reply: May 15, 2003 9:31 AM by ozzie valdos

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
ozzie valdos

Posts: 2
Nickname: ozvaldo
Registered: May, 2003

triangular numbers tester Posted: May 13, 2003 1:41 PM
Reply to this message Reply
Advertisement
i was wondering if someone could give me a little bit of help with my triangular number program.. it is supposed to tell the user if the inputed number is a triangular number or not..the problem is, is that it doesnt seem to work..any help would be greatly appreciated.

public class Triangular {
public static boolean triangular(int number)
{ if (number < 1) return false; // only positive numbers

int tri = 0;
for (int i=1; tri<number; ++i)
{ tri += i; // create next triangular number
} // after the for-loop, tri>=number, with
// tri==number if number is triangular and
// tri>number if number is not triangular

if (tri==number) return true;
return false;

}


public static void main(String[]args) {
int userInput;

//Ask user to supply a number
System.out.println("Please enter a number to see if it is a Triangular Number>");
userInput = (int)KBInput.readDouble(); //Casts the double to an int
System.out.println("The number you entered is " + userInput);
System.out.println("The number " + (triangular(userInput) ? "is " : "is not ") + "a triangular number" );

}
}

//end code when program is finished

thanks again..


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: triangular numbers tester Posted: May 14, 2003 2:38 PM
Reply to this message Reply
import java.io.*;
public class Triangular {
    public static boolean triangular(int number)
    { 
        if (number < 1) return false; // only positive numbers
 
        int tri = 0;
        for (int i=1; tri<number; ++i)
        { 
            tri += i; // create next triangular number
        }   // after the for-loop, tri>=number, with
            // tri==number if number is triangular and
            // tri>number if number is not triangular
 
        return (tri==number);
    }
 
 
    public static void main(String[]args) {
        
 
        //Ask user to supply a number
        int userInput = getInput("Please enter a number to see if it is a Triangular Number>");
        System.out.println("The number you entered is " + userInput);
        System.out.println("The number " + (triangular(userInput) ? "is " : "is not ") + "a triangular number" );
 
    }
    
    public static int getInput(String message){
        int n = -1;
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println(message);
            String s = br.readLine();
            n = Integer.parseInt(s);
        }catch(IOException ioe){
            System.err.println(ioe.getMessage());
        }catch(NumberFormatException nfe){
            System.err.println(nfe.getMessage());            
        }
        return n;
    }
}

ozzie valdos

Posts: 2
Nickname: ozvaldo
Registered: May, 2003

Re: triangular numbers tester Posted: May 15, 2003 9:31 AM
Reply to this message Reply
thanks for the corrections charles. It is appreciated greatly. Thanks again. P.S. You are a credit to this site. Thanks again

Ozvaldo

Flat View: This topic has 2 replies on 1 page
Topic: Stored Procedure return Previous Topic   Next Topic Topic: Using Java to Calculate Semester Hours

Sponsored Links



Google
  Web Artima.com   

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