## 11743 - Credit Check

Moderator: Board moderators

Mizanur Rahman(IUK)
New poster
Posts: 12
Joined: Wed Aug 18, 2010 12:07 pm

### 11743 - Credit Check

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[20]={'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;
}``````
Kamarul Kawnayeen
New poster
Posts: 2
Joined: Wed Feb 02, 2011 10:10 pm
Contact:

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[16], 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;
``````
naseef_07cuet
Learning poster
Posts: 62
Joined: Sat Nov 21, 2009 10:17 pm

Kamarul got it right.I am agree with him.
If you have determination, you can do anything you want....
sarowar_csecu
New poster
Posts: 4
Joined: Wed Nov 04, 2009 6:08 am

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[20];

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;
}
``````
Sarowar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

sarowar_csecu wrote:getting WA! why??
Try the following input:

Code: Select all

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

Code: Select all

``Valid``
Check input and AC output for over 7,500 problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 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
``````
Check input and AC output for over 7,500 problems on uDebug!

Experienced poster
Posts: 122
Joined: Tue Apr 16, 2002 10:07 am

### Re: 11743 - Credit Check

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 };
``````