How to get Digits From a single Long type Variable?

  • Thread starter Thread starter Learner177
  • Start date Start date
L

Learner177

Guest
what would be a better way to achieve below goal

what i want to do is this if i pass 12272358 i want commas like this 1,22,7,2,35,8



here is the code

using System;
using System.Linq;

namespace JobTest
{
class Program
{
public void someFunc(long someVar)
{
var result = someVar.ToString();
var charArray = result.ToCharArray();
int result2 = charArray.Length;

for(int i=0;i<result2;i++)
{
if (i > 0)
{
if( i == 1)
{
if(charArray[0]%2==1 && charArray[1]%2==0)
{
Console.Write(","+charArray);
}
if (charArray[0] % 2 == 0 && charArray[1] % 2 == 1)
{
Console.Write(value: charArray + ",");

}
if(charArray[0] % 2 == 0 && charArray[1] % 2 == 0)
{
Console.Write(value: charArray);
}
if (charArray[0] % 2 == 1 && charArray[1] % 2 == 1)
{
Console.Write(value: charArray);
}

}
else if (charArray % 2 == 0 && i>1)
{
if (charArray[i - 1] % 2 == 0)
{
Console.Write(value: charArray);
}

if (charArray[i-1]%2==1 && charArray%2==1)
{
Console.Write(","+charArray);
}
if (charArray[i - 1] % 2 == 0 && charArray % 2 == 1)
{
Console.Write("," + charArray);
}
if (charArray[i - 1] % 2 == 1 && charArray % 2 == 0)
{
Console.Write("," + charArray);
}

// }
}
else if(charArray % 2 == 1)
{
// Console.Write(",");

if (charArray[i - 1] % 2 == 1)
{
if (charArray[i - 1] % 2 == 0 && charArray % 2 == 1)
{
Console.Write("," + charArray);

}

// Console.Write(value:charArray);
}

if (charArray[i - 1] % 2 == 0)
{
if (charArray[i - 1] % 2 == 0 && charArray % 2 == 1)
{
Console.Write(","+charArray );

}
}
if (charArray[i - 1] % 2 == 1 && charArray % 2 == 1)
{
Console.Write(charArray);
}

}
}
else if(i==0)
{
Console.Write(charArray);
}
}

}
static void Main(string[] args)
{
string line = Console.ReadLine();
long a = long.Parse(line);
Program p = new Program();
p.someFunc(a);
}
}
}

Continue reading...
 
Back
Top