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 :D
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..
num = Convert.ToInt32(Console.ReadLine());
// 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..
Console.ReadKey();
// 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: ");
num = Console.ReadLine();
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));
Console.ReadKey();
}
}
}
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! :D
My first tutorial for beginners from a beginner ; ) let me know what you think!
Edited by Psynic, 09 May 2009 - 01:27 PM.


Sign In
Create Account


Back to top









