Odds and Even Question

RennataDanniela

New member
Joined
Apr 26, 2020
Messages
3
Programming Experience
Beginner
A positive four-digit number is provided. Write a console application to display for each digit, starting from the most important note, whether it is even or odd.
Example:

For input information:

3482

The console will display:

ODD
EVEN
EVEN
EVEN
 
You'll need to make some sort of effort for yourself before there's anything for us to help with. Study your notes and/or text box, do the appropriate research online and then make an attempt to write the code. If you run into a specific problem when you do that, that's probably something that we can help with.
 
My advice is to ask yourself this question:
How would you solve this problem manually without using a computer?

Try to resolve the example you provided manually on a piece of paper. Try to identify the steps you follow and identify patterns.

Then try to translate each of the steps you identified in code one step at a time.

After you tried this for a while and if you struggle, come back here and ask a specific question on a specific step you identified or google it.
 
ok, so I tried to solve it and I almost got, here is the code:

C#:
static void Main(string[] args)
{
    string inputData = Console.ReadLine();
    string inputData1 = Console.ReadLine();
    string inputData2 = Console.ReadLine();
    string inputData3 = Console.ReadLine();
    int a = Convert.ToInt32(inputData);
    int b = Convert.ToInt32(inputData1);
    int c = Convert.ToInt32(inputData2);
    int d = Convert.ToInt32(inputData3);

    if (a%2 ==0)
    {
        Console.WriteLine("PAR");
    }
    else
    {
        Console.WriteLine("IMPAR");
    }

    if (b%2 ==0)
    {
        Console.WriteLine("PAR");
    }
    else
    {
        Console.WriteLine("IMPAR");
    }

    if (c%2 ==0)
    {
        Console.WriteLine("PAR");
    }
    else
    {
        Console.WriteLine("IMPAR");
    }

    if (d%2 ==0)
    {
        Console.WriteLine("PAR");
    }
    else
    {
        Console.WriteLine("IMPAR");
    }
How can I put my input on the same line, like abcd and not
a
b
c
d
?
 
Good start. You are already using Console.ReadLine() and that method allow you to insert all your input in a single line. The method returns a string with all the input in it.
 
You are calling ReadLine four times. If you only want to read one line of input then only call ReadLine once. That will read the entire line of input into a single string variable. You then need to separate it. You might separate the string into chars and then convert each one to a number or you might convert the whole lot to a number first and then separate out the digits. It's up to you but the former is probably easier.
 
You're off to a good start. Just a quick tip.

Don't take user input blindly. It's a good idea to try and parse the user input to whatever value you're expecting.

Ie : string inputData = Console.ReadLine();
int a = Convert.ToInt32(inputData);

 
Notice if you have the following code :
C#:
            string inputData = Console.ReadLine();
            int a = Convert.ToInt32(inputData);
            if (a % 2 == 0)
            {
                Console.WriteLine("PAR");
            }
Line 3 will give you an error if you type in a non numeric value such as Z.
This will result in a System.FormatException

Notice that won't happen in the example below. You should note that if int.TryParse fails to pass the received input from the user, you will not get an exception, and that's because int.TryParse fails silently :
C#:
            string inputData = Console.ReadLine();
            if (int.TryParse(inputData, out int parsed) && parsed % 2 == 0)
            {
                Console.WriteLine("PAR");
            }
You should only use Convert.ToInt32 if you are sure the user input is in fact numeric. No such verifying of the input being numeric is checked below when blindly converting the input received. So I would avoid writing code like this personally :
C#:
            string inputData = Console.ReadLine();
            int parsed = Convert.ToInt32(inputData);
            if (parsed % 2 == 0)
            {
                Console.WriteLine("PAR");
            }
Does this make sense?
 
Notice if you have the following code :
C#:
            string inputData = Console.ReadLine();
            int a = Convert.ToInt32(inputData);
            if (a % 2 == 0)
            {
                Console.WriteLine("PAR");
            }
Line 3 will give you an error if you type in a non numeric value such as Z.
This will result in a System.FormatException

Notice that won't happen in the example below. You should note that if int.TryParse fails to pass the received input from the user, you will not get an exception, and that's because int.TryParse fails silently :
C#:
            string inputData = Console.ReadLine();
            if (int.TryParse(inputData, out int parsed) && parsed % 2 == 0)
            {
                Console.WriteLine("PAR");
            }
You should only use Convert.ToInt32 if you are sure the user input is in fact numeric. No such verifying of the input being numeric is checked below when blindly converting the input received. So I would avoid writing code like this personally :
C#:
            string inputData = Console.ReadLine();
            int parsed = Convert.ToInt32(inputData);
            if (parsed % 2 == 0)
            {
                Console.WriteLine("PAR");
            }
Does this make sense?
Obviously validation is a good thing in the real world but let's not forget that a lot of beginner assignments make the assumption that input will be valid in order that students don't get distracted from what's actually being taught. Post #1 explicitly states that a positive four-digit number is provided so validation is not part of the expectation in this case.
 
Absolutely, I agree. :)

But that doesn't mean we shouldn't teach them the additional lessons which involve validation. While her assignment doesn't ask for validation, It is worth learning in my opinion. I actually disagree with how they teach programming in schools today. But getting back to the actual assignment.
If you only want to read one line of input then only call ReadLine once. That will read the entire line of input into a single string variable.
Given she has 4 variables, I assume she wants 4 different values. So calling ReadLine() will require user input for those other values.
 
Re-read the original problem given as her assignment. She is only supposed to get one input. It was only in post #4 where she is showing her attempt at solving the problem that she introduced 4 different variables and did 4 different calls for input. Furthermore, if you look at her closed thread (because of the language mismatch), the original title was about C# algorithms, and not necessarily about just checking for odd or even. To me this would suggest that the homework is about writing an algorithm that can take a single 4 digit integer, and then displaying whether each of those digits are odd or even.

Unless this assignment is specifically about data structures and algorithms class, I would recommend the following approach:
Input the "number" as a string. (Assume that the input is really an integer and has for digits.)
Iterate over each character of the string:
- convert the character to an actual integer
- check if the integer is odd or even
- display that result

Now, if this assignment is for a data structures and algorithms class, then I am guess that they are currently covering stacks, and so I would recommend the following approach:
Input the "number" as a string.
Parse the number as an integer.
If TryParse() fails, or if the value is less than 1000 or greater 9999 then declare an error.
while the value is not zero do the following:
- push the modulo 10 of the value into a stack
- let the value be value divided by 10
while the stack is not empty do the following:
- pop the value from the top of the stack
- check if the integer is odd or even
- display that result
 
A positive four-digit number is provided. Write a console application to display...
I think you all have misunderstood the assignment. The number is already provided, the task is to write an application to check and display the results. This is provided:
C#:
var input = "3482";
Since a string object is a collection of chars objects you can loop throught it:
C#:
foreach (var digit in input) {
First convert digit to a numeric value, I suggest Char.GetNumericValue method. Then you can check if number is odd/even and display result.
 
She is only supposed to get one input. It was only in post #4 where she is showing her attempt at solving the problem that she introduced 4 different variables and did 4 different calls for input.
That would be my fault. I've had a long week...

Ah that's why JM was calling out about reading the line 4 times. I did't bother to recheck the assignment. Here's the output i got :
C#:
ODD
EVEN
ODD
EVEN
1234

I done it like this and used the CharEnumerator instead :
C#:
        internal static void Request_Response()
        {
            string integer = Console.ReadLine();
            string output = string.Empty;
            using (CharEnumerator char_Value = integer.GetEnumerator())
            {
                while (char_Value.MoveNext())
                {
                    if (int.TryParse(char_Value.Current.ToString(), out int converted_To_Integer))
                    {
                        if (Int_Is_Odd_Or_Even(converted_To_Integer))
                        {
                            Console.WriteLine("EVEN");
                            output = string.Concat(output, converted_To_Integer);
                        }
                        else
                        {
                            Console.WriteLine("ODD");
                            output = string.Concat(output, converted_To_Integer);
                        }
                    }
                    else
                    {
                        throw new FormatException($"Failed to Parse value. { char_Value.Current.ToString() }");
                    }
                }
            }
            int full_Number = Convert.ToInt32(output);
            Console.WriteLine(full_Number);
        }
        public static bool Int_Is_Odd_Or_Even(int value)
        {
            if (value % 2 == 0)
            {
                return true; /* Even */
            }
            else
            {
                return false; /* is odd */
            }
        }
Hopefully this example will help you.
 
Seems awfully convoluted... It could be simplified as:
C#:
string input = "3482";
foreach(char ch in input)
{
    if (Char.IsDigit(ch))
    {
        int value = (int) Char.GetNumericValue(ch);
        // or
        // int value = ch - '0';

        bool isOdd = value % 2 != 0;
        Console.WriteLine(isOdd ? "ODD" : "EVEN");
    }
    else
    {
        Console.Error.WriteLine($"{ch} is not a valid digit.");
    }
}
In this case, there is no major performance gain from getting the enumerator yourself and driving the enumerator.
 
Ah, now that I've stood up, I've got to come back and type again. I now recall: the convoluted approach was deliberate so that code would not just be copied and pasted as an answer. I just gave our OP a freebie. Sorry...
 
Back
Top Bottom