In: Computer Science
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as error in a single digit or switching two digits. The following method is used to verify actual credit card number but, for simplicity, we will describe it for numbers with 8 digits instead of 16: Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then you form the sum 5 + 7 + 8 + 3 = 23. Double each of the digits that were not included in the preceding step. Add all the digits of the resulting numbers. For example, with the number given above, doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27. Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid,. In our case, 23 + 27 = 50, so the number is valid. Write a program that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. If it is not valid, you should print out the value of check digit that would make the number valid.
thanks for help :)
**************************** PLEASE GIVE FEEDBACK IF THEIR IS SOME ISSUE IN CODE ***************
CODE
//C code for checking correct PIN Type
#include <stdio.h>
int sum;
int * digitizedfd(int,int);
int sumvalued(int n1);
int main()
{
int pin_number,sum1,n1,n2,n3,n4,sum2,newsum,numbo,mod;
int *r_arr;
printf("ENTER THE NUMBER");
scanf("%d",&pin_number);
r_arr=digitizedfd(pin_number,8); //Converted to array are all
digits
sum1=r_arr[0]+r_arr[2]+r_arr[4]+r_arr[6]; // Sum part one
n1=r_arr[1];
n1=2*n1; // doubling value
sum2=sumvalued(n1); // summing digits
n2=r_arr[3];
n2=2*n2; // doubling value
sum2=sum2+sumvalued(n2); // summing digits and previous sum
n3=r_arr[5];
n3=2*n3; // doubling value
sum2=sum2+sumvalued(n3); // summing digits and previous
sum
n4=r_arr[7];
n4=2*n4; // doubling value
sum2=sum2+sumvalued(n4); // summing digits and previous
sum
newsum=sum1+sum2; // Adding both the sum
mod=newsum%10; // Checking if it divisible by 10
if(mod==0)
{
printf("\n\n\ncorrect code %d", pin_number);
return 0;
}
else if(r_arr[0]+mod>10) // Making changes to make it divisible
by 10 when overflows digit
r_arr[0]=r_arr[0]-mod; // Making changes to make it divisible by 10
no iverflo this time
else
r_arr[0]=r_arr[0]+mod;
numbo=0;
for(int i=7;i>=0;i--)
numbo=numbo*10+r_arr[i];
printf("\n\n\nIncorrect code , can be corrected as %d", numbo);
return 0;
}
int sumvalued(int n1)
{
sum=0;
while(n1>0)
{
sum=sum+n1%10;
n1=n1/10;
}
return sum;
}
int * digitizedfd(int num,int size)
{
static int arr[8],i=0;
while (num>0)
{
arr[i++]=num%10;
num=num/10;
}
return arr;
}