Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/ISBN13-check-digit/C-sharp/isbn13-check-digit.cs
Normal file
23
Task/ISBN13-check-digit/C-sharp/isbn13-check-digit.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main() {
|
||||
Console.WriteLine(CheckISBN13("978-1734314502"));
|
||||
Console.WriteLine(CheckISBN13("978-1734314509"));
|
||||
Console.WriteLine(CheckISBN13("978-1788399081"));
|
||||
Console.WriteLine(CheckISBN13("978-1788399083"));
|
||||
|
||||
static bool CheckISBN13(string code) {
|
||||
code = code.Replace("-", "").Replace(" ", "");
|
||||
if (code.Length != 13) return false;
|
||||
int sum = 0;
|
||||
foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) {
|
||||
if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3);
|
||||
else return false;
|
||||
}
|
||||
return sum % 10 == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue