Results 1 to 5 of 5
  1. #1
    mazzer07 is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default Java: Armstrong Numbers ?

    Hello Guys,

    I am quite new to java and I have a task to write a program that finds the ARMSTRONG Numbers after executing them. I must make 3 loops and find the armstrong numbers between 100 - 999.

    This is what I have so far and it gaves me an error that variable units doesn't exist, which is not true... Please Help.
    Java Code:
    public class sumreal
    {
     public static void main(String[] args)
     {
      int calculateNums = 0;
      int sumNumCubed = 0;
      int count = 0;
      for (int hundreds = 1; hundreds < 9; hundreds++)
       for (int tens = 0; tens < 9; tens++) 
       
        for (int units = 0; units < 9; units++) 
        
         calculateNums = ((hundreds * 100) + (tens * 10) + units);
         sumNumCubed = (int)Math.pow(hundreds, 3) + (int)Math.pow(tens, 3)
                     + (int)Math.pow(units, 3);
         if (calculateNums == sumNumCubed) 
          count += 1;
         System.out.println(count);
    
     } // main
    
    } // class sumreal

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Java: Armstrong Numbers ?

    gaves me an error that variable units doesn't exist
    Please copy the full text of the error message and paste it here. It has important info about the error.

    One problem I see in the code is that {}s are missing following the if statement and the loops. You should ALWAYS put the statements after an if and loops in {}s
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Java: Armstrong Numbers ?

    An example of what Norm is saying:
    Java Code:
    for(.....)
        int num = 42;
        System.out.println(num); //ERROR
    The error is caused by not having brackets around the for loop. Therefore only one line is inside the loop. The print line is outside the loop where it does not have access to the variable declared inside the loop.

  4. #4
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Java: Armstrong Numbers ?

    for(int i=100;i<=999;i++)
    {
    int n=i;
    while(n>0)
    {
    int k = n %10;
    n = n/10;
    }
    }


    This should give you hint bout how to proceed with your program.Here k is nothing but the last digit in the number

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,539
    Rep Power
    5

    Default Re: Java: Armstrong Numbers ?

    This particular assignment lends itself to Memoization. In fact, once you read up on that topic you may discover a much faster way of implementing this algorithm.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 7
    Last Post: 08-13-2010, 07:02 PM

Posting Permissions

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