Page 1 of 1

### 11743 - Credit Check

Posted: Fri Dec 10, 2010 8:38 pm

Code: Select all

``````#include<stdio.h>
#include<iostream.h>

int main()
{
long int i,n;
cin>>n;
for(i=1;i<=n;i++)
{long int j,sum1,k,count,tmp;
char a={'0'};
sum1=0;
scanf("%[^\n]s",a);getchar();
count=0;j=0;
while(count!=16)
{
if(a[j]!=32)
{
k=int (a[j])-48;
if(count%2==0)
{
k=k*2;
if(k>=10)
{
tmp=k%10;
k=k/10;
k=k+tmp;
}
}
sum1=sum1+k;
count++;
}
j++;
}
if(sum1%10==0)
cout<<"Valid"<<endl;
else
cout<<"Invalid"<<endl;
}
return 0;
}``````

Posted: Thu Feb 03, 2011 12:39 pm
If you can change your approaches to input the card number, then the problem become easy. Try the following code segment for inputing card number:

Code: Select all

``````int credit, j;

for(j=0;j<16;j++)
{
scanf("%1d", &credit[j]);
}``````
Then sum the number in odd index of the array credit[] (i.e. at 1, 3, 5,...15 index) and store it in a variable.
Then sum the 2*number in even index of credit[](i.e. 0, 2, 4, ...14). if 2*number is greater than 9 then try the following code segment:

Code: Select all

``````         sum = sum + (a[j]*2)/10 + (a[j]*2)%10;
``````

Posted: Wed Mar 09, 2011 1:51 am
Kamarul got it right.I am agree with him.

Posted: Tue Sep 20, 2011 9:39 pm
getting WA! why??

Code: Select all

``````#include<stdio.h>

int gd(int n){
n=n*2;
if(n>9)
return n/10+n%10;
else return n;
}

int main(int argc, char** argv) {

int n,i,j,k,f=0,d,ud,s;

int c;

scanf("%d",&n);

while(n--){
d=0;
ud=0;

for(i=0;i<16;i++){
scanf("%1d",&c[i]);
if(i%2==0){
d+=gd(c[i]);
}else
ud+=c[i];
}
s=d+ud;
if(s%10==0)
printf("Valid");
else printf("Invalid");

if(n!=0)
printf("\n");
}

return 0;
}
``````

Posted: Mon Mar 24, 2014 11:58 am
sarowar_csecu wrote:getting WA! why??
Try the following input:

Code: Select all

``````1
8626 1020 0070 7050``````
AC Output:

Code: Select all

``Valid``

Posted: Mon Mar 24, 2014 12:18 pm
Here's some input / output I found useful during testing / debugging.

Input:

Code: Select all

``````9
9999 9999 9999 9999
0000 0000 0000 0000
5050 5050 5050 5050
1234 5678 1234 5678
8931 7853 8957 2362
1000 0000 1001 1009
8326 1829 0478 7841
8626 1020 0070 7050
8726 1020 0070 7050``````
AC Output:

Code: Select all

``````Invalid
Valid
Invalid
Invalid
Invalid
Invalid
Invalid
Valid
Invalid
``````

### Re: 11743 - Credit Check

Posted: Mon Sep 08, 2014 5:53 pm
A small tip to avoid recomputation.

Cache the sum of digits after doubling them:

Code: Select all

``````Digit   Double   Sum of digits
0         0           0
1         2           2
2         4           4
3         6           6
4         8           8
5         10          1
6         12          3
7         14          5
8         16          7
9         18          9

int sumOfDigits[] = { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 };
``````