-1

I have a string with a digit of 4353816274931243 i want to multiply every odd position in the string. So at the end it should look like 831031611221441832283.

I tried to use an if statement like so.

if(cijfer%2==0)
{ 
   cijfer = cijfer*2
}

but this just finds all odd numbers in the string and multiplies them. Any one got any tips on how to solve this problem?

This is the part of code i already have.

string num;
int visanr, cijfer=0;

console.write("Enter visa number: ");
num=console.readline();
num = num.Replace(" ", "");

        visanr = num.Length;

        if (visanr==16)
        {
            for (int i = 0; i < visanr; i++)
            {
                string num1 = num.Substring(i, 1);
                cijfer = int.Parse(num1);

                if (cijfer%2==0)
                {
                    cijfer = cijfer * 2;
                }
            }
        }
        else
        {
            Console.Write("The visanumber " + num + " is not correct!");
        }

        Console.Read();

Thanks in advance

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • This seems like homework. I would suggest try yourself. I can give some idea here. 1. Use the trim function to remove extra space 2. Iterate string like a character array and use for loop lets say index = 0 3. if index / 2 != 0 that index position is odd and parse that str[index] to int and multiply in one variable and keep doing this till the end of the string. – RICKY KUMAR Mar 18 '19 at 19:18
  • You should read this question and it's top answers: https://stackoverflow.com/questions/21249670/implementing-luhn-algorithm-using-c-sharp – Joel Coehoorn Mar 18 '19 at 19:21
  • 1
    Please don't put language names in the question. You've added a language tag to the question, and that's what we'll use to determine what language the question is about; adding the redundant information to the title is just noise. – Eric Lippert Mar 18 '19 at 19:27
  • You want to check odd *positions*, but the value you are checking for oddness is the *value* at that position. Can you explain why you are checking for oddness of the value, instead of the position? – Eric Lippert Mar 18 '19 at 19:29
  • You use `int.Parse`, but what if I typed in `AAAABBBBCCCCDDDD`? What would you expect to happen then? You have to write your program to be robust in the face of bad input; consider using `TryParse`. – Eric Lippert Mar 18 '19 at 19:31
  • @RICKYKUMAR It was indeed school related, not homework but me trying to learn more about programming by doing some extra exercises to get a better understanding. – Hesus De Bodemloze Mar 22 '19 at 12:41

3 Answers3

0

If I understand correctly, cijfer is the number, so if (cijfer%2==0) will find all even number and multiply them.

To find all odd position, i would suggest something like this

for (int i = 0; i < visanr; i++)
{
    string num1 = num.Substring(i, 1);
    cijfer = int.Parse(num1);

    if (i%2==1)
    {
        cijfer = cijfer * 2;
    }
}
maxime bélair
  • 154
  • 1
  • 5
0

Keep in mind that strings start at index 0. So based on your input you actually want to multiply the numbers on the even positions.

Change

if(cijfer%2==0)
{ 
   cijfer = cijfer*2
}

into

if(i % 2 == 0)
{ 
   cijfer = cijfer*2
}
Ian
  • 137
  • 6
0
public static bool CheckLuhnCCNumber(string digits)
{
    return digits.All(char.IsDigit) && digits.Reverse()
        .Select(c => c - 48)
        .Select((thisNum, i) => i % 2 == 0
            ? thisNum
            :((thisNum *= 2) > 9 ? thisNum - 9 : thisNum)
        ).Sum() % 10 == 0;
}

public static void Main()
{       
    Console.Write("Enter visa number: ");
    string num = Console.ReadLine().Replace(" ", "");

    if (num.Length != 16 || !CheckLuhnCCNumber(num))
    {
        Console.Write($"The visa number {num} is not correct!");
    }
    else
    {
        //number length and checksum are OKAY
    }

    Console.ReadKey(true);
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794