Results 1 to 8 of 8
- 08-13-2010, 05:00 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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!! :)
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
LuckJava Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 08-13-2010, 08:32 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
A short description. Dik T Winter solved it all.
kind regards,
Jos
-
- 08-13-2010, 01:37 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-13-2010, 01:39 PM #6
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 ~-'
- 08-13-2010, 01:53 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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"); } } }
- 08-13-2010, 07:02 PM #8
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Programming "MONEY CONVERTER" ???
By Pascal Nouma in forum New To JavaReplies: 1Last Post: 03-25-2009, 08:26 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks