programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# self divisible

Greenhorn
Posts: 19
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.

Bartender
Posts: 11445
18
Welcome to the Ranch.

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

Bartender
Posts: 768
27
A pencil and ruff page, dry run using a loop and Division and Remainder operator might help you to solve this.

Marshal
Posts: 5348
374
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?

Bartender
Posts: 2134
44
Is zero self-divisible?

lowercase baba
Bartender
Posts: 12601
50

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: 2134
44
Of course. I was just wondering whether this method should return false or throw ArithmeticException.

fred rosenberger
lowercase baba
Bartender
Posts: 12601
50

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.

Greenhorn
Posts: 19
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: 2134
44
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.

Greenhorn
Posts: 19
This is my final code.

Paweł Baczyński
Bartender
Posts: 2134
44
• 1
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.

Greenhorn
Posts: 19
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: 5348
374

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?

Greenhorn
Posts: 19
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: 2134
44

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: 12601
50
• 1
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: 2134
44
• 1

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.

 We begin by testing your absorbancy by exposing you to this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS