This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

self divisible  RSS feed

 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm new in java and need help for this method.

An integer is said to be self-divisible if it is divisible by the sum of its digits. For example, both 12 and -12 are self-divisible, since 12 is divisible by 3, which is the sum of the digits 1 and 2. Write a method public boolean isSelfDivisible(int a) that checks whether the parameter a is self-divisible.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

So what have you tried so far and where are you stuck?

 
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
A pencil and ruff page, dry run using a loop and Division and Remainder operator might help you to solve this.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Think for a bit, how you can get individual digits of given number? There are more than one way, but try to find at least one way of getting 1 and 2 separately out of number 12. What would you do?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is zero self-divisible?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Is zero self-divisible?

No.  Division by zero is undefined.  Therefore, you cannot divide by zero.  Therefore, zero is not divisible by the sum of it's digits.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. I was just wondering whether this method should return false or throw ArithmeticException.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Of course. I was just wondering whether this method should return false or throw ArithmeticException.

well then...perhaps you should have asked that question. 
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your comments. This is my code, but i don't know why compiler has an illegal start of type message. I'll appreciate if you help me for both logic and language.

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declared a method inside other method. This is not allowed.

Also, your return statement is wrong.
You can't write if just after return.

It looks like you wrote too much code without trying to compile it.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my final code.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's better now .
You have an unnecessary pair of braces inside your method (lines 4 and 21).

Also, don't write:Write:
Also, it would be much more readable if you had separate methods for getting user input and for calculating a sum of digits.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did some changes. How can i enter mines values as an input?
I mean, when i enter -12, it needs java arithmetic.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashkan ahmadi wrote:This is my final code.
Avoid such sentences until you have strong evidence about your solution corretness

=ashkan ahmadi wrote:For example, both 12 and -12 are self-divisible
How your program works with negative values?

Why you taking user's input inside the method, which supposed to check only if number is self divisible?
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i input negative values, it highlighted line 16 and says in the box that
java.lang.arithmeticexception: /bye zero
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ashkan ahmadi wrote:When i input negative values, it highlighted line 16 and says in the box that
java.lang.arithmeticexception: /bye zero

It's because in case a number is negative, the control will never enter the while loop and sum will remain 0.

You may want to replace num > 0 with num != 0.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another option might be to take the absolute value of the input, since the sign doesn't matter (I think - it's early here and I've not had coffee).
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:another option might be to take the absolute value of the input, since the sign doesn't matter (I think - it's early here and I've not had coffee).

That would work for all values except Integer.MIN_VALUE.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!