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

ORACLE 9i practice solutions

Created by BCL easyConverter SDK 3 (HTML Version)

Zoho Puzzle Questions With Answers

Measuring Time Logic Puzzle You are given with two ropes with variable width. However if we start burning both the ropes, they will burn at exactly same time i.e. an hour. The ropes are non-homogeneous in nature. You are asked to measure 45 minutes by using these two ropes.

How can you do it?

Please note that you can’t break the rope in half as it is being clearly stated that the ropes are non-homogeneous in nature.
Answer & Explanation Solution: 45 minutes

Explanation :
All you have to do is burn the first rope from both the ends and the second rope from one end only simultaneously. The first rope will burn in 30 minutes (half of an hour since we burned from both sides) while the other rope would have burnt half. At this moment, light the second rope from the other end as well. Where, the second rope would have taken half an hour more to burn completely, it will take just 15 minutes as we have lit it from the other end too.

Thus you have successfully calculated 30+15 = 45 minutes …

Hackerrank > SQL > Basic Select

Select
01-Select All
Given a City table, whose fields are described as +-------------+----------+ | Field       | Type     | +-------------+----------+ | ID          | int(11)  | | Name        | char(35) | | CountryCode | char(3)  | | District    | char(20) | | Population  | int(11)  | +-------------+----------+
write a query that will fetch all columns for every row in the table.

My Solution
SELECT*FROM city;
---------------------------------------------------------------------------------
02-Select by ID
Given a City table, whose fields are described as