sfolax6776

Dec 6

A. Introduction A valid credit card number will yield a result divisible by 10 when you:
Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10.
For example, consider the number 4 0 1 2 8 8 8 8 8 8 8 8 1 8 8 1. The sum of all digits is 89. The sum of the bold digits is 46. There are five bold digits larger than four, so the result is 89+46+5=140. 140 is divisible by 10 so the card number is valid. B. Write your code to check if a credit card number is valid or not according to the rules above. You need to verify if the input is a 12-digit number.

Some hints on R6.5:

/* The code below returns sum of 0-4 */

int sum =0; // step 1: define a variable to save the sum

for(int i=0; i<5; i++){ // step 2: using for loop to add one number at a time

sum = sum + i;

}

System.out.printf("The sum of 0 to 4 is: %d \n\n\n", sum); // step 3: print out the sum

/* The code below returns a digit of a number */

int n = 978645312; // original number

int m = n; // make a copy for output

for(int j=8; j>=0; j--){

int digit = n/(int)Math.pow(10,j); // Highest digit is (int)n/10^8

n= n-digit*(int)Math.pow(10,j); // reduce n to get next highest digit

System.out.printf("The 10^%d digit of %d is: %d\n", j, m, digit); // print it out to verify

}

R6.3 a. 1 b. 0 c. 5 d. 2

R6.5 ?

R6.7 a. 9 b. error c. 2 d. 9 e. 8 f. error

R6.21 a. 6 b. 1 c. 6 5

r3.a 1, 3, 6, 10, 15, 21, 28, 36, 45, 55

r3.b 0, 2, 1, 4, 3, 5, 4, 6, 5, 7

r3.c 5, 20, 60, 120, 120

r3.d 1, 2, 8, 64

r7.a 1 2 3 4 5 6 7 8 9

r7.b 1 3 5 7 9

r7.c 10 9 8 7 6 5 4 3 2

r7.d 0 1 2 3 4 5 6 7 8 9

r7.e 1 2 4 8

r7.f 2 4 6 8

r21.a 2 4 7 11 16

r21.b 4 9 16

r21.c 10 7