Skip to main content

Luhn algorithm

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such ascredit card numbersIMEI numbersNational Provider Identifier numbers


Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:
  1. Reverse the order of the digits in the number.
  2. Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1
  3. Taking the second, fourth ... and every other even digit in the reversed digits:
  1. Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits
  2. Sum the partial sums of the even digits to form s2
  1. If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test.
For example, if the trial number is 49927398716:
Reverse the digits:
  61789372994
Sum the odd digits:
  6 + 7 + 9 + 7 + 9 + 4 = 42 = s1
The even digits:
    1,  8,  3,  2,  9
  Two times each even digit:
    2, 16,  6,  4, 18
  Sum the digits of each multiplication:
    2,  7,  6,  4,  9
  Sum the last:
    2 + 7 + 6 + 4 + 9 = 28 = s2

s1 + s2 = 70 which ends in zero which means that 49927398716 passes the Luhn test

#include <string.h>
#include <stdio.h>
 
int luhn(const char* cc)
{
 const int m[] = {0,2,4,6,8,1,3,5,7,9}; // mapping for rule 3
 int i, odd = 1, sum = 0;
 
 for (i = strlen(cc); i--; odd = !odd) {
  int digit = cc[i] - '0';
  sum += odd ? digit : m[digit];
 }
 
 return sum % 10 == 0;
}
 
int main()
{
 const char* cc[] = {
  "49927398716",
  "49927398717",
  "1234567812345678",
  "1234567812345670",
  0
 };
 int i;
 
 for (i = 0; cc[i]; i++)
  printf("%16s\t%s\n", cc[i], luhn(cc[i]) ? "ok" : "not ok");
 
 return 0;
}
Output:
     49927398716        ok
     49927398717        not ok
1234567812345678        not ok
1234567812345670        ok


My Solution ::


int sumDigits(int n){
 //printf("< %d > ",n%9);
  if((n%9)==0)
  return 9;
return (n%9);
}

int chartonum(char ch){

 return (ch-'0');
}


int isValidCredit(char *temp){
int i,sum;
 strrev(temp);
 //puts(temp);
 for(sum = i =0; i<strlen(temp); i++){
  if(i%2==1)
       sum += sumDigits(chartonum(temp[i]) * 2);
  else
       sum += chartonum(temp[i]);
 //printf("%3d",sum);
 }

 if( (sum%10) == 0 )
  return 1;

return 0;
}

Comments

Popular posts from this blog

C Questions

C Questions
C Questions

Note : All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).
Predict the output or error(s) for the following:

void main()
{
int const * p=5; printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
main()
{
char s[ ]="man"; int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm
aaaa nnnn
Explanation

Zoho Interview | Set 1 (Advanced Programming Round)

Third Round: (Advanced Programming Round) Here they asked us to create a “Railway reservation system” and gave us 4 modules. The modules were:
    1. Booking
    2. Availability checking
    3. Cancellation
    4. Prepare chart
We were asked to create the modules for representing each data first and to continue with the implementation phase.

 My Solution :

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<iostream.h>#include<time.h>#include<iomanip.h>#include<fstream.h>char f[10]="f";char s[10]="s";int addr,ad,flag,f1,d,m,i,amt;float tamt; class login {public:char id[100];char pass[100];char*password;void getid(){ cout<<"Enter your id:";gets(id); password=getpass("Enter the password:");strcpy(pass,password);}void displayid(){ cout<<"Id:";puts(id); cout<<"Password:";puts(pass);}}; class detail {public:in…

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)