•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• surajkumardotin

Student college project

• TopHatProductions115

The TXP-Network is coming back this July...

• moonvik

Java...

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Simple console application.

extract console

6 replies to this topic

### #1 Psynic

Psynic

CC Resident

• 67 posts

Posted 08 May 2009 - 06:29 PM

Hey there! this is a neat little console application in c# to seperate a 5 digit number into its individual digits and print out those digits.

i found this challenge in a c# book im working through and its pretty cool if you can figure it out by yourself.

The Problem:
Write a console application that accepts input from the user consisting of 5 digits, and then seperates those digits and prints them individually.

If the user enters the number 42339 it should print out as
* 4
** 2
*** 3
**** 3
***** 9
or you can set up the lines to make it print any way you want.

This can be completed using only the remainder and division operators

Give it a shot. but if u cant figure it out here is the code.

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

namespace welcome
{
public class Program
{
static void Main(string[] args)
{
int num;
// Creates a variable of type integer called "num"

Console.Write("Input a five digit number: ");
// Prompts the user for a 5 digit number..

// Reads and stores the users number in the variable "num"

Console.Write("The seperated digits of the number {0} from your input are:\n", num);
// restates the number from user input..

Console.WriteLine("\n* {0}", (num % 100000 / 10000));
// Finds the last 5 digits in the number and divides the last 4 off leaving the first digit..

Console.WriteLine("\n** {0}", (num % 10000 / 1000));
// Finds the last 4 digits in the number and divides the last 3 off of that..

Console.WriteLine("\n*** {0}", (num % 1000 / 100));
// finds the last 3 digits in the number and divides the last 2 off of that..

Console.WriteLine("\n**** {0}", (num % 100 / 10));
// finds the last 2 digits in the number and divides the last 1 off of that..

Console.WriteLine("\n***** {0}", (num % 10));
// finds the last digit in the number and prints it..

// waits for the user to press a key, to avoid the application closing before you get to see your work in action.
}
}
}```

There you have it.. A simple application to seperate 5 digits from user input.
it is very much so a beginner application.

*NOTE* the only using statements you need to properly create this app is
using system;

Winged panther brought up a good point if u read down a little futher so here is the same application using the substring method instead
```using System;

namespace command_line
{
class Program
{
static void Main(string[] args)
{
string num = "";

Console.Write("Enter a 5 digit Number: ");

Console.WriteLine("First digit: {0}", num.Substring(0,1));
Console.WriteLine("Second digit: {0}", num.Substring(1, 1));
Console.WriteLine("Third digit: {0}", num.Substring(2,1));
Console.WriteLine("Fourth digit: {0}", num.Substring(3,1));
Console.WriteLine("Fifth digit: {0}", num.Substring(4,1));

}
}
}```

first you create a string and name it what you want(in this case "num")
then you prompt the user to enter the number using console.write()
then you assign the users input to num using the console.readline function.
after that you display each digit/character using writeline.

in Console.WriteLine your text you want the user to see goes in the quotation marks along with {0} <---- what the 0 does in the curly braces is basically marks where in your output it is going to display the arguement.

in this case the argument comes after the comma in
Console.WriteLine("First digit: {0}", num.Substring(0,1));
num.Substring(0,1) is the argument.

num.Substring(0,1) works like this. num is the string you made earlier..
when you use .substring it is going to read the string the user input. in the brackets specifies which character in the the string it is going to start reading at and how many characters long it is going to read for. in this case it starts at the first character(starts at 0,1,2,3,4 for a 5 digit number) and it is going to read 1 character long. effectively extracting the first digit out of your string. from there on you rinse and repeat until you have your 5 digits displaying seperately!

console.readkey is somethin i use just so the app doesnt close before i can see what it did.

the neat thing about this method is you can use it to seperate anything the user inputs not just numbers! thanks again wingedpanther!

My first tutorial for beginners from a beginner ; ) let me know what you think!

Edited by Psynic, 09 May 2009 - 01:27 PM.

• 1

### #2 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 09 May 2009 - 04:33 AM

Not bad, I've got a question, though: if the input is text, why not use text functions to break it apart?
• 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #3 Psynic

Psynic

CC Resident

• 67 posts

Posted 09 May 2009 - 12:07 PM

partially because i havent learned to do that yet by all means if you could elaborate then we could learn how to do it that way. im interested in seeing how that works.
• 0

### #4 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts
• Location:Upstate, South Carolina
• Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
• Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 09 May 2009 - 12:16 PM

After a quick search, it looks like the substring method would do the job quite nicely.
• -1

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #5 Psynic

Psynic

CC Resident

• 67 posts

Posted 09 May 2009 - 12:26 PM

Yes i found that but for some reason i cant get substring to work i must be missing a using statement : /

oo wait i think i got it thanks wingedpanther im going to write an example of this code and add it on. thanks for the idea!

Edited by Psynic, 09 May 2009 - 12:33 PM.
durr i found it lol

• 0

### #6 cismarel

cismarel

CC Newcomer

• Just Joined
• 24 posts

Posted 01 June 2009 - 09:08 AM

Thanks
• 0

### #7 PrakashJ

PrakashJ

CC Lurker

• Just Joined
• 1 posts
• Programming Language:C#
• Learning:C#

Posted 24 July 2014 - 10:34 PM

public void methInputTreeTricks()

{

string num = "";

Console.Write("Enter a 5 digit Number: ");

for (int i = 0; i < num.Length; i++)

{

for (int j = 0; j <=i; j++)

{

Console.Write("*");

}

Console.WriteLine(num.Substring(i, 1));

}

}

.......Enjoy

• 0

### Also tagged with one or more of these keywords: extract, console

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download