Credit card numbers follow certain pattern. A credit card number must be 16 digits, and must start with

• 4 for Visa cards

• 5 for Master cards

• 37 for American Express cards

• 6 for Discover cards

Suppose that you were asked to write down a Java program that validates the credit card number (if it is correct or not!). Here are the rules that determine the correctness of credit card number:

Suppose the credit card number = 4388576018402625

1) Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single digit number:

2*2 = 4

2*2 = 4

4*2 = 8

1*2 = 2

6*2 = 12 (1 + 2) = 3

5*2 = 10 (1 + 0) = 1

8*2 = 16 (1 +6) = 7

4*2 = 8

2) Now add all single-digit number from step 1 (4+4+8+2+3+1+7+8 = 37)

3) Add all digits in the odd places from right to left in the card number:

5+6+0+8+0+7+8+3 = 37

4) Sum the results from step 2 and 3

37 + 37 = 74

5) If the result in step 4 is divisible by 10, then credit card is valid, else it is not valid.

74 is not divisible by 10, so it is not valid!

Write a java program to accept a credit card number from keyboard as long, and then prints on screen, if it is valid or not. You should use the following:

public static int[] fillArray(long) : this method fills the credit card number in an array

public static int getSum(int[]) :calculated the summation from step 1 and 2

Public static int getOddSum(int[]): calculates the summation from step 3

Public static boolean isValid(int, int): determines if valid or not according to step 5.

without usin object oriented

This post has been edited by **pbl**: 28 September 2010 - 04:25 PM

Reason for edit:: Topic title changed to be more meaningful