-5

I have this java project(using eclipse) that calculates check digit numbers. I'm stuck on how to code this one.

The check has a check digit so that it makes the sum of the sights including the check digit divisible by 7. Assume there are always 4 digits plus the check digit. The sample is 3875 with the number being 5 Second trial problem is 5862 and check number needs to be found. How do I go about doing this? I got to entering each digit and adding them but how can i do the rest?

This is for an into to computer science class so please no super complex stuff as if we didn't learn it I cant use it.

My teacher sucks by the way we learn none of this. I already did part a I need part b. Thanks. Here's an image to hell clarify.

Question

Frakcool
  • 10,915
  • 9
  • 50
  • 89
user2860253
  • 31
  • 1
  • 8
  • 2
    And what have you tried? The instructions aren't even clear enough, 5+8+6+2 = 21, it's divisible by 7, I don't know what you want the program to do. For better help sooner please describe your question, and post a valid [mcve] with examples of desired and actual input-output. Also please take the [tour], go through the [help] and learn [ask] – Frakcool Jan 04 '17 at 22:55
  • 2
    Maybe pay more attention in class? – OldProgrammer Jan 04 '17 at 22:55
  • Pretty sure we're not supposed to help with homework problems. – jaredgilmore Jan 04 '17 at 22:59
  • 1
    If you got no idea where to start, you're in the wrong place. If you're stuck in the middle with a specific problem, we'll be glad to help, but you have not told us what your specific problem is. Also show what you have tried. – Ole V.V. Jan 04 '17 at 23:02
  • @OleV. V I done current have a computer but from what I can remember is I asked for the input of each number individually than added those up but had no idea how to continue – user2860253 Jan 04 '17 at 23:12
  • @Frakcool I Apologize for that. The numbers must be added thab a special number must be calculated that when added to the sum of the digits is divisible by 7. In my example it must be one. I feel stupid now. It makes more sense that you said that. I must be overthinking it – user2860253 Jan 04 '17 at 23:15
  • Alright, so it sounds to me that the problem is in the math? You've done 5 + 8 + 6 + 2 = 21, and now you need to find some check digit `check` such that 0 <= `check` <= 9 and `21 + check` is divisible by 7? – Ole V.V. Jan 04 '17 at 23:23
  • @OleV.V. Yes sounds right. It just had to work for any number combination but sounds like ot should – user2860253 Jan 04 '17 at 23:28
  • If you have not done so, go read about the modulo operator, written with a percent sign. `n % 7` will give you the remainder of `n` in a division by 7. Think about whether and how this may help you. – Ole V.V. Jan 04 '17 at 23:30
  • @OleV.V.I'm faintly familiary with the % mod. Not fully though so I'll read up on it. Well say you get the 23 and use the mod you get 2 as an output. So add that output to the sum of digits? – user2860253 Jan 04 '17 at 23:36
  • Try to do the calculation and see if you get the correct result in the end. You're on the right track, maybe there's one more simple calculation you will need to do before reaching your goal. Also see Frakcool's answer, looks helpful to me. – Ole V.V. Jan 04 '17 at 23:45
  • @OleV.V.Thank you so much! I feel so stupid its not that bad when you think about it like that. Tomorrow in going to try this all out and see! – user2860253 Jan 05 '17 at 00:13
  • If the answer solved your question, please accept it – Frakcool Jan 05 '17 at 00:46

1 Answers1

3

First of all, you need to develop some "programmer logic", these problems help to develop it.

Airline tickets divide the actual identification number by 7 and assign the remainder to the check digit. Number can be of any length

Example:

12358 #3

Let's break this example:

12358 / 7 = 1765

and the reminder is 3

Let's do the same with the 2nd number on the example:

45349 / 7 = 45346

and the reminder is 3

So, your logic is correct.

An American Express traveler's check has a digit so that it maskes the sum of the digits, including the check digit, evenly divisible by 7.

Example:

3875 #5

In this problem the thing is a little different, you need to sum the digits:

3875 -> 3 + 8 + 7 + 5 = 23

Now you need to get the reminder of 23 / 7

23 / 7 = 3

And a reminder of 2

7 - 2 = 5

That's your checkDigit

5862

5862 -> 5 + 8 + 6 + 2 = 21

21 / 7 = 3

Reminder = 0

checkDigit = 7 - 0 = 7

So the formula is:

  1. Split the number into digits
  2. Sum the digits
  3. Get the mod 7 of the sum
  4. Make a rest of 7 - reminder
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Quite elegant. Helping without doing the asker's homework for him or her. – Ole V.V. Jan 04 '17 at 23:38
  • @OleV.V. As I have no code to check, the most I can do is give him the logic since that's what OP's asking for actually, and thanks – Frakcool Jan 04 '17 at 23:39
  • 1
    Thank you both. By no means did i want the entire thing done done for me, just explained and maybe a little code. Sorry i didnt have any code, next time i will make sure I have it. – user2860253 Jan 05 '17 at 20:01