Results 1 to 8 of 8
  1. #1
    vinzal2010 is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Arrow help me with my java programming "armstrong number"...

    it should be 100-999 is the Armstrong number but my codes only determine 1-9...

    I'm using eclipse
    here's the code:

    import java.util.Scanner;

    public class Armstrong {

    static double finalValue = 0;

    public static void main(String[] args) {

    int valueTaker = 0;

    System.out.println("Enter a number to be checked for Armstrong Number: ");

    Scanner armstScan = new Scanner(System.in);

    if(armstScan.hasNextInt())
    {
    valueTaker = armstScan.nextInt();
    }

    StringBuffer sb = new StringBuffer().append(valueTaker);

    double lengthSBKeeper = sb.length();

    for(int lengthKeeper = 0; lengthKeeper < sb.length(); lengthKeeper++)
    {
    double zxc = Integer.parseInt(sb.substring(lengthKeeper, lengthKeeper+1));

    finalValue = Math.pow(zxc, lengthSBKeeper) + finalValue;

    }

    if(finalValue == valueTaker)
    {
    System.out.println("The entered number "+valueTaker+" is an Armstrong Number");
    }
    else
    {
    System.out.println("The entered number is not an Armstrong Number");
    }
    }
    }


    can u analyze it??
    pls help me with my java programming... thanks!! :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) What is an Armstrong number?
    2) What steps have you taken to debug your code? Have you run it through a debugger? Do you use println statements to check state of variables at critical steps in the code?
    3) When posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Luck

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    1) What is an Armstrong number?
    A short description. Dik T Winter solved it all.

    kind regards,

    Jos

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    A short description. Dik T Winter solved it all.
    Thanks as always Jos. Actually, I've already looked it up, but I felt (and still feel) strongly that it is the OP's responsibility to take the effort to explain this concept to all if he's going to ask others to help him with this project.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Thanks as always Jos. Actually, I've already looked it up, but I felt (and still feel) strongly that it is the OP's responsibility to take the effort to explain this concept to all if he's going to ask others to help him with this project.
    Yep, you're right (of course); posters should do their best to exaplain their problem as clearly as possible; none of us (well, except me ;-) are mind readers and we can't google for every unclear question and guess what the OP had meant ...

    kind regards,

    Jos

  6. #6
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Where do you set the start and finish number?
    So your suppose to do it to the power of three correct?
    I feel like your math is wrong

    3POWER(3) + 7POWER(3) + 1POWER(3) = 371.

    I'd personally write this completely different.

    place a System.out.println(sb.length());
    why are you going by a length for looping and powering? Isn't this where you set to 999?
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  7. #7
    vinzal2010 is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default like this sir?

    Java Code:
    import java.util.Scanner;
    
    public class Armstrong {
    
    static double finalValue = 0;
    
    public static void main(String[] args) {
    
    int valueTaker = 0;
    
    System.out.println("Enter a number to be checked for Armstrong Number: ");
    
    Scanner armstScan = new Scanner(System.in);
    
    if(armstScan.hasNextInt())
    {
    valueTaker = armstScan.nextInt();
    }
    
    StringBuffer sb = new StringBuffer().append(valueTaker);
    
    double lengthSBKeeper = sb.length();
    
    for(int lengthKeeper = 0; lengthKeeper < sb.length(); lengthKeeper++)
    {
    double zxc = Integer.parseInt(sb.substring(lengthKeeper, lengthKeeper+1));
    
    finalValue = Math.pow(zxc, lengthSBKeeper) + finalValue;
    
    }
    
    if(finalValue == valueTaker)
    {
    System.out.println("The entered number "+valueTaker+" is an Armstrong Number");
    }
    else
    {
    System.out.println("The entered number is not an Armstrong Number");
    }
    }
    }

  8. #8
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Works for me!

    Java Code:
    Enter a number to be checked for Armstrong Number: 
    371
    The entered number 371 is an Armstrong Number
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Programming "MONEY CONVERTER" ???
    By Pascal Nouma in forum New To Java
    Replies: 1
    Last Post: 03-25-2009, 08:26 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •