-1

I'm new to c# language and still learning. I have to write a console application (like example) but the real problem for me is that I don't know how to access each number for the input integer and pass it on the division function (n % 2 ==0) and return even/odd for the given number.

For example: the user input: 4444 and the console result: even even even even

        string inputData = Console.ReadLine(); // the 4 digit number
        int number = Convert.ToInt32(inputData);
        string emptyStr = String.Empty;
        string divided = "even";
        string notDivided = "odd";
       // here .......????
        if (number % 2 == 0)
        {
            Console.WriteLine(divided);
        }
        else
        {
            Console.WriteLine(notDivided);

        }
  • You can convert the integer to a string, loop over its characters and convert each char back to an int. – SomeBody Jul 31 '20 at 08:40
  • You can either use /10 and %10 to extract digits one by one, but that's usually easiest to do right-to-left not left-to-right, or you can use .ToString(). – Rup Jul 31 '20 at 08:40
  • 1
    I think this will answer your question https://stackoverflow.com/questions/829174/is-there-an-easy-way-to-turn-an-int-into-an-array-of-ints-of-each-digit – Joeri E Jul 31 '20 at 08:42
  • 1
    " the user input: 4444" the user cannot input an `int` into a program. It will always be a string! In this case you can iterate with a loop over the string convert each character into an `int` (please don't forget to call `ToString()` on the character!!!) and feed it as n into your function – Mong Zhu Jul 31 '20 at 08:44
  • 1
    please post your entire code, we can then pint point directly your problems and help you to solve them – Mong Zhu Jul 31 '20 at 08:47
  • 1
    Please show what code you have so far (eg, where you get the user input) and I'm sure someone can show you how to turn each character typed to an integer. – Jamiec Jul 31 '20 at 08:47
  • Thank you all for your input :) I will try some of them in a short time. I put the code that I have – keeponfalling Jul 31 '20 at 10:26

3 Answers3

1

I think this is what you are looking for.

First of all use this Is there an easy way to turn an int into an array of ints of each digit?

After that just check every individual item in your array

public void main
{

   var numbers = NumbersIn(987654321).ToArray();
   foreach (int element in numbers)
   {
      if (element % 2 == 0)
      {
         Console.Write("even");
      }
      else
      {
         Console.Write("uneven");
      }
   }
}
        

public Stack<int> NumbersIn(int value)
{
    if (value == 0) return new Stack<int>();

    var numbers = NumbersIn(value / 10);

    numbers.Push(value % 10);

    return numbers;
}
Joeri E
  • 111
  • 9
0

https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=netcore-3.1 you can use the hyperlink provided to use the method Console.ReadLine method for the conditional output that you are C#-ing.

  • 3
    Thanks for taking the time to contribute, however it's advised not to post links as answers. In a couple of years somebody might stumble upon this question and the link might not work anymore. So quote the relevant parts from a link and add some context and story around it. Linking to extra information can be fine but an answer should be complete without reliance on external sources. –  Jul 31 '20 at 09:05
  • Thank you for your interest in my participation. I will try to focus more on what the right answer based more on code and lesser on hyperlinks examples. – Marian Alexandru Aug 02 '20 at 14:51
-1

You can read each key written by user in loop with:

var digit = Console.ReadKey()

Remember to parse char to int properly.

As Mong Zhu noticed, you cannot parse char value with: Int32.Parse('1'). You have to do it with string value.

So full solution is:

var digitStr = Console.ReadKey();
var digit = Int32.Parse(digitStr.KeyChar.ToString());
zolty13
  • 1,943
  • 3
  • 17
  • 34