Account numbers sometimes contain a check digit that is the result of a mathematical calculation. It is used to help ascertain whether the number is a valid one. Develop a simple C# application that asks a user to enter a four-digit account number and determines whether it is a valid number. The number is valid if the fourth digit is the remainder when the number represented by the first three digits of the four-digit number is divided by 7. For example, 7770 is valid, because 0 is the remainder when 777 is divided by 7.
using System;
class Program
{
static void Main(string[] args)
{
string str = "";
int value = 0;
int account_number;
int check_digit;
while (value != 1)
{
Console.Write("Please enter a four-digit account number (from 1000 to 9999) or enter 1 to exit:");
str = Console.ReadLine();
if (int.TryParse(str, out value))
if (value >= 1000 && value <= 9999)
{
account_number = value / 10;
//Console.WriteLine("account_number " + account_number);
check_digit = value % 10;
//Console.WriteLine("check_digit " + check_digit);
//Console.WriteLine("account_number % 7 " + account_number % 7);
if (account_number % 7 == check_digit)
Console.WriteLine("Account number " + value + " is valid");
else
Console.WriteLine("Account number " + value + " is not valid");
}
}
}
}
Comments
Leave a comment