• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enter a number and print if it's an Armstrong number or not : help me remove the error.  RSS feed

 
Akshat Shree Ram Pandey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there good guys!
Long time since I asked a question,
I tried to make an armstrong number checker but all the time it prints " Not an armstrong number "
Here's the code


So.. can anyone please point out my mistake??
PS : I'm forced to use for loop :/
 
Akshat Shree Ram Pandey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I myself got my mistake.
I've written ld = n%10 whereas it should be i%10;
Sh*t, how do I delete this?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We generally don't delete stuff.  We prefer to leave it here so others can learn from everyone's mistakes.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's good you found the solution yourself.
  • I think, you can create a separate method rather than writing It in main method. Worth reading Main Is A Pain
  • Why are you casting on line no 15? Here
  •  
    Campbell Ritchie
    Marshal
    Posts: 55759
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Avoid Math#pow for small whole number arguments; you are probably better off with
    ld * ld * ld
    rather than
    Math.pow(ld, 3)
     
    Akshat Shree Ram Pandey
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:It's good you found the solution yourself.
  • I think, you can create a separate method rather than writing It in main method. Worth reading Main Is A Pain
  • [b]Why are you casting on line no 15? Here
  • [/b]

    I'm casting it since x is double and I can't use relational operator == with a double and an int right, so I'm converting it to int so it's compared and the boolean value is thus obtained.
    Well I love main method, IDK why it seems easy instead of using other objects
     
    Akshat Shree Ram Pandey
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Avoid Math#pow for small whole number arguments; you are probably better off with
    ld * ld * ld
    rather than
    Math.pow(ld, 3)

    Oki thanks for the tip ^^
    PS : how'd that double post occur o.O
     
    Piet Souris
    Rancher
    Posts: 1983
    67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What if you enter a four digit number?
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akshat Shree Ram Pandey wrote:I'm casting it since x is double and I can't use relational operator == with a double and an int right, so I'm converting it to int so it's compared and the boolean value is thus obtained.
    x? I think you meant sum anyway, You can use equality operator (== Or != ) with a double and an int.

    If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible to numeric type, binary numeric promotion is performed on the operands.
    Binary numeric promotion says:
  • When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type.
  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

  • and more...

    Here double sum and int num are of numeric type so binary numeric promotion is performed where widening primitive conversion is applied and int num is converted to double implicitly. You don't need to convert It explicitly. 
     
    Henry Wong
    author
    Sheriff
    Posts: 23283
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akshat Shree Ram Pandey wrote:
    I'm casting it since x is double and I can't use relational operator == with a double and an int right, so I'm converting it to int so it's compared and the boolean value is thus obtained.


    Actually, comparing a double and an int works fine. The compiler will implicitly cast the int to a double, and the two double values are compared.

    Casting the double to an int actually makes the comparison less accurate. For example, if the double value was 3.789 and the int value was 3, the cast of the double will truncate the double value to 3; and hence, the compare will return true.

    I will speculate that your program requires less precision, and/or is encountering too many round-off errors, that the comparison is returning false, when it should be true. On the other hand, casting to an int, IMHO, may be an overkill. After all, do you really want something like 3.789 and 3 to be equal?

    Henry
     
    Campbell Ritchie
    Marshal
    Posts: 55759
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akshat Shree Ram Pandey wrote:. . . Oki thanks for the tip ^^
    That's a pleasure

    PS : how'd that double post occur o.O
    Don't know, but I have got rid of it.
     
    Piet Souris
    Rancher
    Posts: 1983
    67
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OP already solved his problem (second post).

    The Math.pow is usually accurate enough for the purpose at hand, however OP is failing the definition of an Armstrong number (therefore my question).
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!