locked
Use every second integer RRS feed

  • Question

  • I cant explain what i want exactly but heres an example of what i want to do.

    Its not homework, its a personal project. I gave up on coding during my first year, so im not very good at it.

    Would appreciate the help!

    //Using Number 800121500908(5) as an example:
    
    //Add all the digits in the odd positions (excluding last digit).
    //8 + 0 + 2 + 5 + 0 + 0 = 15  .............................[1]
    
    //Move the even positions into a field and multiply the number by 2.
    //011098 x 2 = 22196  .....................................[2]
    
    //Add the digits of the result in [2].
    //2 + 2 + 1 + 9 + 6 = 20  .................................[3]
    
    //Add the answer in [3] to the answer in [1].
    //15 + 20 = 35  ...........................................[4]
    
    //Subtract the second digit of [4](i.e. 5) from 10. The number must tally with the last number in the example(5).
    //If the result is 2 digits, the last digit is used to compare against the last number in the example Number. 
    //If the answer differs, the example number is invalid.


    Mr.SMP

    Tuesday, May 13, 2014 1:28 PM

Answers

  • I have manged to reach till [4].

     long input = 800121500908;
                Console.WriteLine(string .Format("[Input] is {0}",input));
    
                string inputAsString = input.ToString();
    
                int index = 0;
                int oddDigitsS =0;
                long evenDigitsNumber;
                long evenDigitsNumberDoubled;
                string evenDigitsString = string.Empty;
    
                foreach (char digitChar in inputAsString)
                {
                    index++;
    
                    if (index % 2 == 1)
                    {
                        oddDigitsS += int.Parse(digitChar.ToString());
                    }
                    else
                    { 
                        evenDigitsString+= digitChar.ToString();
                    }
                }
                Console.WriteLine(string .Format("[1] is {0}",oddDigitsS));
    
                evenDigitsNumber = long.Parse(evenDigitsString);
    
                evenDigitsNumberDoubled = evenDigitsNumber * 2;
                Console.WriteLine(string .Format("[2] is {0}",evenDigitsNumberDoubled));
    
                int evenDigitsNumberDoubledSum = evenDigitsNumberDoubled.ToString().Select(d => int.Parse(d.ToString())).ToList().Sum();
                Console.WriteLine(string .Format("[3] is {0}",evenDigitsNumberDoubledSum));
    
                int sum = oddDigitsS + evenDigitsNumberDoubledSum;
                Console.WriteLine(string.Format("[4] is {0}", sum));

    //Subtract the second digit of [4](i.e. 5) from 10. The number must tally with the last number in the example(5).
    //If the result is 2 digits, the last digit is used to compare against the last number in the example Number. 
    //If the answer differs, the example number is invalid.


    I don't really understand the meaning of above steps. Please explain.


    TejasJ.

    • Marked as answer by Mr.SMP Wednesday, May 14, 2014 7:40 AM
    Tuesday, May 13, 2014 2:27 PM

All replies

  • I have manged to reach till [4].

     long input = 800121500908;
                Console.WriteLine(string .Format("[Input] is {0}",input));
    
                string inputAsString = input.ToString();
    
                int index = 0;
                int oddDigitsS =0;
                long evenDigitsNumber;
                long evenDigitsNumberDoubled;
                string evenDigitsString = string.Empty;
    
                foreach (char digitChar in inputAsString)
                {
                    index++;
    
                    if (index % 2 == 1)
                    {
                        oddDigitsS += int.Parse(digitChar.ToString());
                    }
                    else
                    { 
                        evenDigitsString+= digitChar.ToString();
                    }
                }
                Console.WriteLine(string .Format("[1] is {0}",oddDigitsS));
    
                evenDigitsNumber = long.Parse(evenDigitsString);
    
                evenDigitsNumberDoubled = evenDigitsNumber * 2;
                Console.WriteLine(string .Format("[2] is {0}",evenDigitsNumberDoubled));
    
                int evenDigitsNumberDoubledSum = evenDigitsNumberDoubled.ToString().Select(d => int.Parse(d.ToString())).ToList().Sum();
                Console.WriteLine(string .Format("[3] is {0}",evenDigitsNumberDoubledSum));
    
                int sum = oddDigitsS + evenDigitsNumberDoubledSum;
                Console.WriteLine(string.Format("[4] is {0}", sum));

    //Subtract the second digit of [4](i.e. 5) from 10. The number must tally with the last number in the example(5).
    //If the result is 2 digits, the last digit is used to compare against the last number in the example Number. 
    //If the answer differs, the example number is invalid.


    I don't really understand the meaning of above steps. Please explain.


    TejasJ.

    • Marked as answer by Mr.SMP Wednesday, May 14, 2014 7:40 AM
    Tuesday, May 13, 2014 2:27 PM
  • Hope this helps as start..do add exception handling and modify as needed..Welcome bk to forums after two years..

      var input = "8001215009085".ToCharArray();
            var first = input.Where((x, i) => (i % 2 == 0 && i != input.Length - 1)).Select(x => x - '0').ToArray();
            var firstresult = first.Sum(x => x); // 1
            Console.WriteLine(firstresult);
            var second = Convert.ToInt32(String.Join("", input.Where((x, i) => (i % 2 == 1 && i != input.Length - 1)).Select(x => x - '0')));
            var secondresult = second * 2; //2
            Console.WriteLine(secondresult);
            var third = secondresult.ToString().ToCharArray().Where((x, i) => (i == i)).Select(x => x - '0').ToArray();
            var thirdresult = third.Sum(x => x);
            Console.WriteLine(thirdresult);
            var fourthresult = firstresult + thirdresult; //4
            Console.WriteLine(fourthresult);
            var result =  10 - Convert.ToInt32(fourthresult.ToString().ElementAt(1) - '0');
            if (result == Convert.ToInt32(input.ElementAt(input.Length - 1) - '0'))
            {
                Console.WriteLine("matched");
            }

    Tuesday, May 13, 2014 5:36 PM
  • I have manged to reach till [4].

     long input = 800121500908;
                Console.WriteLine(string .Format("[Input] is {0}",input));
    
                string inputAsString = input.ToString();
    
                int index = 0;
                int oddDigitsS =0;
                long evenDigitsNumber;
                long evenDigitsNumberDoubled;
                string evenDigitsString = string.Empty;
    
                foreach (char digitChar in inputAsString)
                {
                    index++;
    
                    if (index % 2 == 1)
                    {
                        oddDigitsS += int.Parse(digitChar.ToString());
                    }
                    else
                    { 
                        evenDigitsString+= digitChar.ToString();
                    }
                }
                Console.WriteLine(string .Format("[1] is {0}",oddDigitsS));
    
                evenDigitsNumber = long.Parse(evenDigitsString);
    
                evenDigitsNumberDoubled = evenDigitsNumber * 2;
                Console.WriteLine(string .Format("[2] is {0}",evenDigitsNumberDoubled));
    
                int evenDigitsNumberDoubledSum = evenDigitsNumberDoubled.ToString().Select(d => int.Parse(d.ToString())).ToList().Sum();
                Console.WriteLine(string .Format("[3] is {0}",evenDigitsNumberDoubledSum));
    
                int sum = oddDigitsS + evenDigitsNumberDoubledSum;
                Console.WriteLine(string.Format("[4] is {0}", sum));

    //Subtract the second digit of [4](i.e. 5) from 10. The number must tally with the last number in the example(5).
    //If the result is 2 digits, the last digit is used to compare against the last number in the example Number. 
    //If the answer differs, the example number is invalid.


    I don't really understand the meaning of above steps. Please explain.


    TejasJ.

    You are awesome!! Thanks a Lot!

    Mr.SMP

    Wednesday, May 14, 2014 7:40 AM
  • TejasJ, I got the last part right on my own thanks.

    All it had to do was take the second digit (5) from the answer (35) in [4] and subtract it from 10.

    If the answer in [4] would be 41, the second digit would be 1 and subtracted from 10 and the answer would be 9.

    I know this isnt part of the question but while im at it..

    the problem you solved for me right now is integer z

    long a, xy, z, count;
                
    
           a = 41061800000;
           xy = 8;
           z = 0;
           count = 41061800000;
                
    
    
        asdf:
    
           if (count <= 41061803800)
           {
              a = count;
              count = count + 10;
    
              Console.Write(a);
              Console.Write(xy);
              Console.Write(z);
    
    
              Console.WriteLine("");
              goto asdf;
            }
    
            Console.ReadLine();

    each count of a, xy must be calculated like you showed me to get z.

    i should be able to do that. just want to know how i can get each count of a, xy as a single integer(long) (axy) instead of two (a) (xy)


    • Edited by Mr.SMP Wednesday, May 14, 2014 9:31 AM
    Wednesday, May 14, 2014 9:28 AM