fierflash
fierflash

Reputation: 125

C# - Separate integers/strings with division and remainder

I have a homework that I just can't figure out how to do. I'm not sure if the teacher wrote it wrong(he is a bad typer) or if there is a way to accomplish the task.

I work in Visual C# 2010 Express - Console Application

My task is to:

Read a four digit integer, such as 5893, from the keyboard and display the digits separated from one another by a tab each. Use both integer division and modulus operator % to pick off each digit. If the user enters 4567, the output looks like:

4567

4 5 6 7

Sure I know how to separate the numbers by using \t as well as reading the input and displaying it to the user. But how am I supposed to 'pick off' each digit with division and the remainder operators? Maybe he means something else, but not sure.

And another question...

How do I make sure that what the user types in is a number and not a letter? Do I have to use Char.IsLetter, because I couldn't figure out how to use it on a parsed string.

Example:

        string number1;
        int x;
        Console.Write("Please enter a number to calculate: ");
        number1 = Console.ReadLine();
        x = Int32.Parse(number1);

What method am I supposed to use and where do i put it in? Because now i only get an error and the application shuts down if I try to enter e letter.

Upvotes: 1

Views: 5020

Answers (5)

Christopher Govender
Christopher Govender

Reputation: 555

For everybody new to C#, the solution can be simpler.

        // Variables
        int number = 1234;
        int remainder;
        string s = "";

        while (number > 0) {
            remainder = number % 10;
            s = remainder + "\n" + s;
            number /= 10;
        }
        Console.Write (s);

Upvotes: 0

veljkoz
veljkoz

Reputation: 8514

Ok, I won't give the whole solution (after all, this is homework ;)).

This code would check if there's four characters in input:

        string input;
        do
        {
            input = Console.ReadLine();
            if (input.Length != 4)
                Console.WriteLine("You have to enter FOUR digits");
        } while (input.Length != 4);

This could be one way of checking the digits:

        bool isOk = true;
        foreach (char c in input.ToArray())
        {
            if (!Char.IsDigit(c))
            {
                Console.WriteLine("You have to enter four DIGITS");
                isOk = false;
                break;
            }
        }

This approach doesn't deal with math but you could do that just as well:

int num = int.Parse(input);
int tensOfThousands = num / 10000;
int thousands = (num - tensOfThousands) / 1000;
int lastDigit = num % 10;
//the rest of the digits are up to you ;)

Upvotes: 0

EMP
EMP

Reputation: 61971

The first question is really more about maths than programming. You know what the division and modulus operators do. Think about how you could use them to get the last (least significant) digit. Once you've done that you can apply a similar technique for the 2nd digit (tens) and then the same for hundreds and thousands and so on.

You've already found Char.IsLetter, but there is also Char.IsDigit, which does what you want more directly. You can do this check for one character and what you have is a string of characters. Have a look at a foreach loop.

Upvotes: 5

Grokys
Grokys

Reputation: 16526

Yes, your assignment makes sense. Say you have an integer x = 4567. To pick out the digit at position A you would use:

result = (int)((x / (A * 10)) % 10);

The first part of this (x / (A * 10)) "shifts" x to the right. So a value of A = 1 would divide 4567 by 10 which results in 456.7. You then use the modulo operator "%" to pick out the unit part of that number. Finally you convert to an int to remove the fractional part.

Upvotes: 0

Crag
Crag

Reputation: 458

System.Int32.TryParse() might be a better choice when converting the String.

Upvotes: 1

Related Questions