stupid java question

Discuss all aspects of programming here.

Moderator: The Mod Squad

stupid java question

Postby Saphery » Wed Sep 22, 2004 9:00 am

Hey guys I am doing an assignment for Java and I need to find the last digit in a value entered by the user.

for example:
If the user entered: 12344321566
i need to find the last 6
then divide the full # by 7 to see if the remainder is = to 6

even the smallest help is appreciated. thanks
Posts: 12
Joined: Wed Sep 01, 2004 9:55 am

Postby dr_st » Wed Sep 22, 2004 9:46 am

The last digit of a number is its remainder (modulus) when divided by 10.

In Java, like in C and C++, the % operator is used for remainder.

So, if your number is stored in a variable of type int called num, this will give you the last digit:


And this will give you the remainder when divided by 7:


So, your condition would be:

if (num%7==6) ......
Black Belt 2nd Degree
Black Belt 2nd Degree
Posts: 2191
Joined: Tue May 11, 2004 11:18 pm
Location: Outworld

Postby thomas_w_bowman » Wed Sep 22, 2004 9:46 am

So, an assignment...I'll try to help without doing it for breaking down some implicit functional requirements:

User-entered value has a length, set a variable to that length-1

set a one-character to the value at (length-1)

Check that single-character variable for valid numeric value (yes, you may have defined input as numeric, but this way it'll never crash because of any change(s).)

Convert the character to a numeric value, divide it by seven (or "if less than 7 - it's already a remainder - else if it's 7 or more subtract 7 and the result is a remainder). For larger intigers, divide by seven - forcing answer to a integer-only value, then multiply that value by 7 and subtract it from the original number to obtain the remainder (making sure that you use integer type numerics for all of this, it would get messy if using a floating point number or any decimal places).

Now turn the requirement into Java.
Better living thru technology...
"Open the Pod Bay Doors, HAL..."
Join Folding team #: 33258
Black Belt 2nd Degree
Black Belt 2nd Degree
Posts: 2884
Joined: Fri Feb 28, 2003 2:59 pm
Location: Minneapolis, MN

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest