TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
gemma browse
NA
10
61.4k
Luhn’s algorithm
Dec 14 2010 8:02 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
////namespace parta
namespace LuhnCardValidate
{
public partial class Frmcard : Form
{
public Frmcard()
{
InitializeComponent();
}
public static bool validateluhn(String cardNumber);
public void button1_Click(object sender, EventArgs e)
{
if (button1.Enabled == true)
{
card();
}
}
public void card()
{
string cardnumber = "5191 7011 9192 1874";
if (validateluhn(cardnumber))
{//changed because have to use form not console though cant test to see if what im doing works!
lblvalid.Text = ("Valid"); //was console.WriteLine. . .
this.Text = txtnum.Text; //was console.Read();
}
else
{
Console.WriteLine("Not Valid");
Console.Read();
}
}
public static bool validateluhn(String cardNumber)
{
//Clean the card number- remove dashes and spaces
// Hint use the Replace method for this
//Convert card number into digits array
int[] digits = new int[cardnumber.Length];
for (int len = 0; len < cardnumber.Length; len++)
{
digits[len] = Int32.Parse(cardNumber.Substring(len, 1));
}
//Luhn's Algorithm
//Hans Peter Luhn (July 1, 1896 – August 19, 1964)
int sum = 0;
bool alt = false;
for (int i = digits.Length - 1; i >= 0; i--)
{
int currentDigit = digits[i];
if (alt)
{
currentDigit *= 2;
if (currentDigit > 9)
{
currentDigit -= 9;
}
}
sum += currentDigit;
alt = !alt;
}
//If Mod 10 equals 0, the number is good and this will return true
return sum % 10 == 0;
}
}
theres an error sayin that the parta.frmcard already defines a member called validateluhhn with the same parameter types is this because the way i have saved my work? any help greatly many thanks
Reply
Answers (
1
)
inserting a passwork to a form on c#
Getting all the XML paths from a XML file.