Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.80/5 (6 votes)
See more:
ok i've been set a task to write a small C# program and heres the brief

"An Albanian social security number has a sequence of digits
followed by a letter. This 'check' letter is determined from
the values of the digits and is used to ensure that the numbers
are correct. Write a program that, when given a ten-digit
number and letter as input, calculates a unique 'check'
character corresponding to the given number according to the
following rules:

Add the five pairs of digits contained in the number
Take the remainder of dividing the result by 26
Select the letter in that position of the alphabet, starting
with 'A' in position 0, 'B' in position 1, and so on.

For example, if the input is 1122334455J, the program should
calculate (11 + 22 + 33 + 44 + 55) % 26 giving 9, and so 'J'
is the 'check' character. This means that the social security
number quoted is correct. If the input was 1122334455M, your
program should detect that the check letter is incorrect,
and therefore that this social security number is false."

This is what ive got so far

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CourseWork
{
    class Program
    {
        static void Main(string[] args)
        {
            string socialNumber;
            char check;


            Console.WriteLine("please Write your social number without the letter."); //asks user to input social number without letter
            string first = Console.ReadLine();

            socialNumber = first.Substring(0, 10);
            check = first[first.Length-1];
            Console.WriteLine(check+socialNumber);
            
            int sum = 0;
            for (int i =0; i < socialNumber.Length-1; i++)
            {
                sum += Int32.Parse(socialNumber[i].ToString());
            }

            int remainder = sum % 26;


            if (remainder == check)
            {
                Console.WriteLine("This is a correct number.. the check is {0} \\n the remainder is {1}", check, remainder);
            }

            else
            {
                Console.WriteLine("Please write your check letter.");
            }
            Console.WriteLine(sum);
            Console.ReadLine();

        }
    }
}


So far i've asked the end user to input their social security number without the check letter.

What i can't get my head around is the division doesn't work and always returns the number 25 or it just closes the program. I don't know where to go from here >_< can somebody help me?
Posted
Updated 16-Jan-14 16:24pm
v3
Comments
Philippe Mori 16-Jan-14 22:30pm    
Learn to uses a debugger and to read spécifications (5 pairs of number).
BillWoodruff 17-Jan-14 4:16am    
Add #65 to remainder to offset for the start of upper-case characters. See my response below.

While it is true that chars map to integer values, and can be compared with integer values directly without error, somehow I feel more comfortable converting to a character and comparing.

Does that (conversion) make the code more readable, or express "intent" more clearly ? Is there any possible "gotcha" if the character is not an ASCII character, but a multi-byte Unicode character ? ... You decide.

Example:
C#
string socialNumber = "1122334455J";

int sum = 0;

for (int i = 0; i < 5; i++)
{
  sum += Convert.ToInt32(socialNumber.Substring(0,2));
  socialNumber = socialNumber.Remove(0,2);
}

// at this point there's one character left in 'socialNumber ... if the string was valid

bool isValidSSNumber = Convert.ToChar((sum % 26) + 65) == socialNumber[0];
Note that in production code you would definitely want to have much more comprehensive validation on the input string.
 
Share this answer
 
v4
Comments
Rob Philpott 17-Jan-14 4:58am    
Nice. I think you'd be surprised how few people know that an 'A' is 65. I suspect you could replace 65 in your code with 'A' actually (implicit cast?) but would have to try it to confirm..
Try this code,

C#
class Program
    {
        static void Main(string[] args)
        { 
            string input = "1122334455J";
            int sum = 0;
            if (input.Length == 11)
            {
                for (int i = 0; i < 5; i++)
                     sum += Convert.ToInt32( input.Substring(i*2,2 ));

                bool isValidNumber = ((sum % 26) + 65) == input[input.Length - 1];
                Console.WriteLine(input + " is a {0} Number ", isValidNumber ? "valid" : "not valid");
            }
            Console.ReadLine();
        }
    }


Modifed as per Ron comment
Removed LINQ
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 16-Jan-14 21:22pm    
Add using System.Linq; reference..
Ron Beyer 16-Jan-14 21:29pm    
string.Join("", input.Skip(i * 2).Take(2).Select(k => k.ToString()).ToArray()

Super complicated way to say:

input.Substring(i*2, 2)
Karthik_Mahalingam 16-Jan-14 21:35pm    
sorry i will change it..
Ron Beyer 16-Jan-14 21:36pm    
Plus, you don't need the list, just use an int and += the parse, no need to use Linq at all in this problem, which I'm sure is way beyond where his class is.
Karthik_Mahalingam 16-Jan-14 21:45pm    
removed linq :)
You're simply summing up the numbers. The algorithm requires that you sum the pairs of numbers - i.e. 5 numbers, each containing 2 digits. IOW, change the right hand side of the line sum +=. You're very close to a solution.

/ravi
 
Share this answer
 
v2
Comments
Member 10506451 16-Jan-14 17:03pm    
honestly Ravi, I have no idea what im doing.... could you possibly walk me through what I have to do?
Ravi Bhavnani 16-Jan-14 17:45pm    
idenizeni has provided you a full solution below. I corrected their response - the upper bound check was off by one.
In you loop, when you sum your pairs you need to take two at a time...
C#
 // interate values two at a time
for (int i = 0; i < socialNumber.Length; i = i + 2)
{
    string strFirstDigit = socialNumber[i].ToString(); // get value 1 from pair
    string strSecondDigit = socialNumber[i + 1].ToString(); // get value 2 from pari
                
    // concatenate string values into a single value, convert to int and add to sum
    sum += Int32.Parse(strFirstDigit + strSecondDigit);
}


Corrected the upper bound of the loop [Ravi Bhavnani]
 
Share this answer
 
v2
Comments
BillWoodruff 17-Jan-14 3:02am    
This code has an obvious crashing error indicating to me you did not test it.
idenizeni 17-Jan-14 16:53pm    
I did test it and it didn't crash. Why does it crash on you?
As an alternative: you might convert the plain number into a long first (not int, since int cannot hold 10-digit numbers), and then divide by 100 (takes the last two digits as reminder). There is a convenient function to do simultanously div and rem in one go.

C#
...
long nr = 0;
if (!long.TryParse(socialNumber.Substring(0, 10), out nr)) { /*todo: error handling */ return; }
long sum = 0;
while(nr != 0)
{
    long rem = 0;
    nr = Math.DivRem(nr, 100, out rem);
    sum += rem;
    sum %= 26;
}
...// sum == (check-'A') --> OK, else error

Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900