views:

235

answers:

5

Determine if all the digits of the sum of n -numbers and swapped n are odd. For example: 36 + 63 = 99, y 409 + 904 = 1313. Visual Studio builds my code, there is still something wrong with it ( it doesnt return an answer) can you please help me here?

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            long num = Convert.ToInt64(Console.Read());
            long vol = voltea(num);
            long sum = num + vol;

            bool simp = simpares(sum);


            if (simp == true)
                Console.Write("Si");
            else
                Console.Write("No");

        }

       static private bool simpares(long x)
        {
            bool s = false;
             long [] arreglo  = new long [1000];
             while ( x > 0)
            {
             arreglo [x % 10] ++;
             x /=10;
            }

           for (long i=0 ; i <= arreglo.Length ; i++)
           {
               if (arreglo [i]%2 != 0)
                   s = true;
           }
            return s;
        }
       static private long voltea(long x)
        {
           long v = 0;

             while (v > 0) 
         {
               v = 10 * v + x % 10;
                 x /= 10;

            }
            return v;
      }




    }



}
A: 

What are you able to see on console screen. According to me it should be 'si' or 'no'

harshit
Sorry, its all in spanish. "Si" YES ( if its digits are all odd) and "No" is NO ( if not all its digits arent odd). I should see one of these answers in my screen.
Juan
A: 

Please try to figure out homework questions on your own. Will help you think different. Good luck!

Raj Kaimal
@Raj: SO permits asking for help with homework questions, but strongly suggests the student show what they have tried, exactly where they are stuck and generally that they have made an honest effort. I agree those conditions are not given here. See http://meta.stackoverflow.com/questions/10811/homework-on-stackoverflow
Eric J.
A: 

It looks to me like you might have an infinite loop and a loop that never enters.

// because v = 0, the while will never execute
long v = 0;

while (v > 0) 
{
    v = 10 * v + x % 10;
    x /= 10;
}
Joel Etherton
x is an integral type, so it will be truncated and eventually reach 0. What makes you think otherwise?
Ben Voigt
@Ben Voigt - honestly I don't know. Reading my response again I'm getting a very wtf vibe.
Joel Etherton
I believe the confusion was because you're initial post looked like you were saying the problem was in the "simpares" function, when in fact you're pointing to a problem in the "voltea" function. The v in the while conditional should be x.
McAden
+6  A: 

I'm not sure what's wrong with your code, but I was thinking an easier way to accomplish this would be to use strings, rather than doing all the divisions and mods by 10.

  1. Convert original number to string, reverse the string, then convert that back to a long
  2. Add the original and reversed numbers
  3. Convert the sum to a string
  4. Loop over the result string and check to see if each digit is odd
Andy White
+1, teaching to fish.
Eric J.
+3  A: 

It's not too clear what you mean by "Doesn't return an answer".

Add:

        Console.ReadKey();
        Console.ReadLine();

At the end of your Main function. I'd hazard a guess that you're not seeing an answer because the console is closing on you.

EDIT:

Found it:

for (long i=0 ; i <= arreglo.Length ; i++)

Index out of bounds. That should be:

for (long i=0 ; i < arreglo.Length ; i++)

i should be "Less than" arreglo, not "Less than or equal to"

EDIT2:

This is why your current code is broken. I'd highly recommend also looking at alternative methods of solving the problem. See Andy White's Answer.

McAden