-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhn.c
128 lines (117 loc) · 2.83 KB
/
luhn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<stdio.h>
//declare function prototype of sum1 and sum2
int sum1(int[],int);
int sum2(int[],int);
//start of the program
int main(){
//size store the size of the varable while num[20] stores numbers
int size,num[20];
//initialize size to 0;
size=0;
//prompt the user in a loop
int temp;
printf("\nEnter digit to terminate -1\n");
while(temp>=0)
{
printf("Enter %d\n",(size+1));
scanf("%d",&temp);
//dont store -1 in the array
if(temp>=0)
{
num[size]=temp;
//increase size by 1
size += 1 ;
}
}
//print all the card numbers
printf("\nCredit card number is ");
int i;
for(i=0;i<size;i++){
printf("%d",num[i]);
}
// stores sum 1 and 2;
int sum[2];
sum[0]=sum1(num,size);
printf("\nSum 1 is %d",sum[0]);
sum[1]=sum2(num,size);
//print the result of sum1 and sum 2
printf("\nSum 2 is %d",sum[1]);
//print checkdigit
//calculate check digit
int checkdigit=((sum[0]+sum[1])*9)%10;
printf("\nCheck digit is %d",checkdigit);
printf("\nLast digit on credit card is %d",num[size-1]);
//check if checkdigit calculated is same as the last digit
if(checkdigit==num[size-1])
{
printf("\ncheck sum %d and the last digit %d are the same: valid credit card number",checkdigit,num[size-1]);
}
else{
printf("\ncheck sum %d and the last digit %d are the NOT same: INVALID credit card number",checkdigit,num[size-1]);
}
printf("Enter a key to exit");
char c;
c = getchar();
}
int sum1(int num[],int size){
int sum=0;
int i;
//a temporary array to store the sum 1 numbers
int temp[size];
printf("\nNumbers in sum 1 are ");
for(i=(size-1-1);i>=0;i=i-2)
{ int twice;
twice = 2 * num[i];
if(twice>9)
{
// find the remainder so as to find the last digit
int temp1 = twice % 10;
//find the first number after minusing the remainder
int temp2 = (twice-temp1) / 10;
//add the sum
sum = sum + temp1 + temp2;
}
else{
//add the twice
sum = sum + twice;
}
temp[i]=num[i];
}
int x;
//print sum 1 numbers the way they are in the credit number
for(x=0;x<size;x++)
{
if(temp[x]==num[x])
{
printf("%d ",num[x]);
}
}
//return sum to the main
return sum;
}
int sum2(int num[],int size){
//we store the sum and return it later
int sum=0;
int i;
//a temporary array to store the sum 1 numbers
int temp[size];
printf("\nNumbers for sum2 are ");
for(i=(size-1);i>=0;i=i-2)
{
if(i!=size-1)
{
sum = sum +num[i];
temp[i]=num[i];
}
}
int x;
for(x=0;x<size;x++)
{
if(temp[x]==num[x])
{
//print the numbers the way they are in ascending order in the credit card
printf("%d ",num[x]);
}
}
return sum;
}