Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ValidatonNumberCopy1
- {
- class Program
- {
- public static int RemainingDaysToBirthday(DateTime birthday)
- {
- var nextBirthday = birthday.AddYears(DateTime.Today.Year - birthday.Year);
- if (nextBirthday < DateTime.Today)
- {
- nextBirthday = nextBirthday.AddYears(1);
- }
- return (nextBirthday - DateTime.Today).Days;
- }
- static void Main(string[] args)
- {
- try
- {
- bool correctCount = true;
- bool correctMonth = true;
- bool correctSixthSeventh = true;
- bool correctValidationNumber = true;
- bool correctDayOfBirth = true;
- string monthStr = "";
- int month = 0;
- string yearLastDigitsStr = "";
- int yearLastDigits = 0;
- string dateOfBirthStr = "";
- int dayOfBirth = 0;
- long[] arrayDigits = new long[0];
- do
- {
- Console.Write("Enter EGN: ");
- string EGNStr = Console.ReadLine();
- //string EGNStr = "9203127630";
- int digitsCount = EGNStr.Length;
- // Check for exactly 10 digits
- correctCount = true;
- if (digitsCount != 10)
- {
- correctCount = false;
- continue;
- }
- long EGN = long.Parse(EGNStr);
- arrayDigits = new long[digitsCount];
- for (int i = 0; i < arrayDigits.Length; i++)
- {
- arrayDigits[(arrayDigits.Length - 1) - i] = EGN % 10;
- EGN = EGN / 10;
- }
- // Check for correct month
- monthStr = EGNStr.Substring(2, 2);
- month = int.Parse(monthStr);
- correctMonth = ((month > 0 && month < 13) || (month > 20 && month < 33) || (month > 40 && month < 53));
- if (!correctMonth)
- {
- correctMonth = false;
- continue;
- }
- // Check for correct date of birth
- dateOfBirthStr = EGNStr.Substring(4, 2);
- dayOfBirth = int.Parse(dateOfBirthStr);
- if (!(dayOfBirth > 0 && dayOfBirth < 32))
- {
- correctDayOfBirth = false;
- continue;
- }
- // Check for sixth and seventh digit
- string sixthAndSeventhDigitStr = EGNStr.Substring(6, 2);
- int sixthAndSeventhDigit = int.Parse(sixthAndSeventhDigitStr);
- correctSixthSeventh = true;
- if (!(sixthAndSeventhDigit != 0 && sixthAndSeventhDigit != 88))
- {
- correctSixthSeventh = false;
- continue;
- }
- // Check validation number
- int[] validationDigits = { 2, 4, 8, 5, 10, 9, 7, 3, 6 };
- long validationSum = 0;
- for (int i = 0; i < validationDigits.Length; i++)
- {
- validationSum = validationSum + (arrayDigits[i] * validationDigits[i]);
- }
- long validationNumber;
- long residue = validationSum / 11;
- if (residue > 10)
- {
- validationNumber = 0;
- }
- else
- {
- validationNumber = validationSum / 11;
- }
- correctValidationNumber = (validationNumber == arrayDigits[9]);
- yearLastDigitsStr = EGNStr.Substring(0, 2);
- yearLastDigits = int.Parse(yearLastDigitsStr);
- } while (!(correctCount && correctMonth && correctSixthSeventh && correctValidationNumber && correctDayOfBirth));
- Console.WriteLine("Personal information: ");
- Console.Write("- Gender: ");
- string gender = "";
- if (arrayDigits[8] % 2 == 0)
- {
- gender = "male";
- }
- else
- {
- gender = "female";
- }
- Console.WriteLine(gender);
- int yearOfBirth = 0;
- if (month > 0 && month < 13)
- {
- yearOfBirth = 1900 + yearLastDigits;
- }
- else if (month > 20 && month < 33)
- {
- yearOfBirth = 1800 + yearLastDigits;
- month = month - 20;
- }
- else
- {
- yearOfBirth = 2000 + yearLastDigits;
- month = month - 40;
- }
- DateTime date = DateTime.Now;
- int currentYear = date.Year;
- int age = currentYear - yearOfBirth;
- Console.Write("- Age: ");
- Console.WriteLine(age);
- Console.Write("- Date of birth: ");
- DateTime birthday = new DateTime(yearOfBirth, month, dayOfBirth);
- string format = "dd MMMMMMMMMM yyyy";
- Console.WriteLine(birthday.Date.ToString(format));
- Console.Write("- Days remaining to the next birthday: ");
- int remainingDays = RemainingDaysToBirthday(birthday);
- Console.WriteLine(remainingDays);
- }
- catch (FormatException)
- {
- Console.WriteLine("You have entered a number containing some characters.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement