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:
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.
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.
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 :
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 :
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 :
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 :
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.
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.
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
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.
Seems awfully convoluted... It could be simplified as:
C#:
string input ="3482";foreach(char ch in input){if(Char.IsDigit(ch)){intvalue=(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...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.