# breaking up a number

Page 1 of 1

## 13 Replies - 827 Views - Last Post: 29 November 2013 - 10:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=335106&amp;s=d69880d64db02a6a3012f67911f26592&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bkruep

Reputation: -1
• Posts: 29
• Joined: 26-July 13

# breaking up a number

Posted 27 November 2013 - 09:45 AM

I am working on some exercises in a C# book I purchased and hoping to get some assistance. I was able to find the code and it works but I have a couple questions...

My instructions are to Write an app that inputs one number consisting of five digits from the user, separating the number into its individual digits and displays the digits.

Here is the code that worked...

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DigitsInteger
{
class Program
{
static void Main(string[] args)
{
//Declare variables;
int num;

//Get the value of the number
Console.WriteLine("Enter a number with a 5 digits.");
Console.WriteLine("Put three spaces in between each number");
Console.WriteLine();

Console.Write("Enter a number with 5 digits: ");

num = int.Parse(inputNumber);
Console.Write(string.Format("{0}   {1}   {2}   {3}   {4}", (num / 10000) % 1000, (num / 1000) % 10, (num / 100) % 10, (num / 10) % 10, num % 10));

Console.WriteLine();

}
}
}

```

Here are some of my questions...

First in the code...
```Console.Write(string.Format("{0}   {1}   {2}   {3}   {4}", (num / 10000) % 1000, (num / 1000) % 10, (num / 100) % 10, (num / 10) % 10, num % 10));

```

I was getting two numbers for the second number until I changed it from % 100 to %10 and just trying to understand why it should be this.

My other question is could someone explain to me this code as I found this when searching google and just trying to just get a quick explanation. I understand that the num is divided by 1000 20 get a reminder of 1000 but after that I am just confused which I believe goes back to my first question. If this is confusing of what Im asking if someone could just disregard my specific questions and explain the lines below it would be most helpful for me moving forward....

```
num = int.Parse(inputNumber);
Console.Write(string.Format("{0}   {1}   {2}   {3}   {4}", (num / 10000) % 1000, (num / 1000) % 10, (num / 100) % 10, (num / 10) % 10, num % 10));

```

This post has been edited by modi123_1: 27 November 2013 - 09:48 AM
Reason for edit:: fixed botched code tags

Is This A Good Question/Topic? 0

## Replies To: breaking up a number

### #2 modi123_1

• Suitor #2

Reputation: 10482
• Posts: 40,450
• Joined: 12-June 08

## Re: breaking up a number

Posted 27 November 2013 - 09:50 AM

Quote

[...]
I was able to find the code and it works
[...]
My other question is could someone explain to me this code as I found this when searching google
[...]

Side question - if you are using this book to learn but you circumvent the actual application of your learning by just finding a solution on the internet and asking for people to explain it to you - versus working through the problem and learning how to build the solution on your own - then what's the point?

### #3 bkruep

Reputation: -1
• Posts: 29
• Joined: 26-July 13

## Re: breaking up a number

Posted 27 November 2013 - 10:16 AM

In the 15 chapter exercises I found 1 that I had trouble finding much information on the rest I worked out no problem. So if you want to go on a power trip on one damn app that I could not find much information on go ahead but all I was asking help on to grasp the information on one problem not 15! I do not have an instructor to go to and I was not asking for code to be written for me and I know this is easy for you so I figure a 2 or 3 sentence explanation would have been simple not a post about making a comment just to put someone else down trying to learn a new language.

Let me also add I will never ask this site for any code or help with code but if I do ever find code that I dont understand and there is not much in my book to go with yes I am going to ask for a quick explanation. And to Modi I apologize for the last message but I just thought if I am not asking for code and couldnt found much information in my book I am not sure where else I can go to get an explanation.

### #4 modi123_1

• Suitor #2

Reputation: 10482
• Posts: 40,450
• Joined: 12-June 08

## Re: breaking up a number

Posted 27 November 2013 - 10:17 AM

You seemed to misunderstand my question. I found the course of action.. interesting... and was looking for clarification on the motivation. There is minimal 'power trip' involved.

Quote

and I was not asking for code to be written for m

Certainly - since you found it written elsewhere! (sorry - couldn't resist)

### #5 bkruep

Reputation: -1
• Posts: 29
• Joined: 26-July 13

## Re: breaking up a number

Posted 27 November 2013 - 10:22 AM

OK that was my fault then and I apologize. If someone can just give me some advice if I cant find much information on a concept in my book and when I run a google search on it all I can find is code that is already written (without much explanation), where should I go to find help? I do not have an instructor to ask and just trying to find the best way to understand this problem as I am trying to understand the language and not just finish exercises any way possible.

### #6 andrewsw

• I'm not here to twist your niblets

Reputation: 4529
• Posts: 16,674
• Joined: 12-December 12

## Re: breaking up a number

Posted 27 November 2013 - 12:49 PM

At the risk of upsetting you further:

Quote

My instructions are to Write an app that inputs one number consisting of five digits from the user, separating the number into its individual digits and displays the digits.

How will you write your application? How will you approach splitting a five-digit number into five single digits?

That code obviously isn't helping you, because you don't understand it. So disregard it.

### #7 astonecipher

• Dishonored

Reputation: 1149
• Posts: 4,847
• Joined: 03-December 12

## Re: breaking up a number

Posted 27 November 2013 - 01:41 PM

This is basically a math exercise. take the number 123. What happens when it is divided by 100?

Next you have to remember something called integer division. It means all we will use is the whole number. The next lesson in using % or Modulus. % is used when you care about the remainder only. 5 % 20 = 5.

123 / 100 = num
num % 10 = ?

Essentially what the code does is pull a number from each position (1000's, 100's, 10's, 1's). This is the math used to get the spot value. And I do expect you to answer the math problem!

### #8 Skydiver

• Code herder

Reputation: 4100
• Posts: 13,080
• Joined: 05-May 12

## Re: breaking up a number

Posted 27 November 2013 - 02:00 PM

Ummmm... Isn't everybody assuming that the chapter is about arithmetic operations? If this were a chapter about strings, then the solution is so much simpler.
```string inputNumber = Console.ReadLine();
foreach(char ch in inputNumber)
Console.Write(ch);

```

### #9 astonecipher

• Dishonored

Reputation: 1149
• Posts: 4,847
• Joined: 03-December 12

## Re: breaking up a number

Posted 27 November 2013 - 02:16 PM

I could be wrong. It has happened before and will happen again.

### #10 Skydiver

• Code herder

Reputation: 4100
• Posts: 13,080
• Joined: 05-May 12

## Re: breaking up a number

Posted 27 November 2013 - 02:46 PM

The main reason why I bring up manipulating the number as a string vs. manipulating it as an integer is later when the classic exercise about palindromic numbers comes up. It is much easier to check for palindromes by treating the number as string than it is to try to inspect each digit by doing divs and mods.

And speaking of palindromes, anybody notice that questions about palindromes comes up often in the C/C++ forum, but not in the C# forum? I wonder why.

### #11 tlhIn`toq

• Not here as much anymore

Reputation: 6012
• Posts: 13,189
• Joined: 02-June 10

## Re: breaking up a number

Posted 27 November 2013 - 02:48 PM

Quote

one number consisting of five digits from the user, separating the number into its individual digits and displays the digits.

I agree with Skydiver. The instructions say NOTHING about doing any math on the user input. Just to break it up into its individual digits. That's string manipulation in my interpretation.

I also agree the user is trying to *find* an answer someplace rather than comprehending the point of the exercise and instructions to *create* a solution. Can't see the forest through all the trees.

### #12 astonecipher

• Dishonored

Reputation: 1149
• Posts: 4,847
• Joined: 03-December 12

## Re: breaking up a number

Posted 27 November 2013 - 02:53 PM

I agree as well. I was attempting to explain how the code he published worked. And while string manipulation is easier his had math, which to me turned it into a math problem.

What book are you using to learn C#?

I wonder if SkyDiver's palindrome question resolves that many newer comers here are more interested in the game dev side than anything else?

This post has been edited by astonecipher: 27 November 2013 - 02:54 PM

### #13 bkruep

Reputation: -1
• Posts: 29
• Joined: 26-July 13

## Re: breaking up a number

Posted 29 November 2013 - 09:50 AM

Sorry for the late response. The book I am using is Visual C# 2012. This is only the third chapter which is Arithmetic operations. While I do understand strings we have not gotten there yet and just want to work on what ive learned up to each point in the book.

### #14 Skydiver

• Code herder

Reputation: 4100
• Posts: 13,080
• Joined: 05-May 12

## Re: breaking up a number

Posted 29 November 2013 - 10:07 AM

In that case, using mods and divs would be the correct approach for that exercise.

Did you cover loops yet in chapter 3? If not, then let recommend doing the following, instead of computing each digit at repetitively:

In pseudo-code:
```first digit = quotient of num divided by 10000
num = remainder of num divided by 10000
second digit = quotient of num divided by 1000
num = remainder of num divided by 1000
third digit =  quotient of num divided by 100
num = remainder of num divided by 100
fourth digit =  quotient of num divided by 10
num = remainder of num divided by 10
fifth digit = num

```

The key here is that you are reassigning the remainder back to the number to be examined.