|
hi
I am ArunKumar,i am a beginner to .NET
if user enters input integer value as :5678
then output should be :5
6
7
8
** Please dont use input variable as string,input variable is int
for ex:
int x=5678; not as string x=5678;
thanks,
ArunKumar.
|
|
|
|
|
In .NET, you can easily convert the integer to string, then get each digit out. So in your example,
int x=5678;
you can declare a string variable
string sx=x.ToString();
After that you just need to use SubString() to extract each digit out of it.
|
|
|
|
|
[Just because you said you don't wanna use string]
use a different complicated process: (just for fun)
int x = 5678;
List<int> values = new List<int>();
while (x > 0)
{
int value = x - ((int)(x / 10) * 10);
values.Add(value);
x = (int)(x / 10);
}
You get all the values in the list but in reverse order. So you can use
values.Reverse();
|
|
|
|
|
int x = 5678;
string asText = string.Format("{0:0 0 0 0}", x);
string[] parts = asText.Split(' ');
for (int i = 0; i < parts.Length; i++)
{
int value = Convert.ToInt32(parts[i]);
}
EDIT ================
This is the 2nd time this week this question has come up. One of the Indian universities must have started up another semester.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
foreach(char c in x.ToString()) Console.WriteLine(c);
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
he didn't want to use a string - check my next answer...
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Here's the "pointless math exercise" version:
int value = 12345;
double i = 1;
double power = 0;
List<int> digits = new List<int>();
while (value > 0)
{
power = Math.Pow(10d, i);
digits.Add(value % (int)power);
value = (int)((double)value / power);
}
digits.Reverse();
EDIT =======================
This is clearly a homework assignment, and to be brutally honest, I see no future for you in programming. If you can't analyze and solve such a simple math problem, you have no business working as a programmer. Yeah, I provided you with the answer, but don't mistake this for a kindness on my part. In fact, I strongly advise you to share this code with your classmates, because if I see this particular request again, I cannot guarantee that I will be kind *or* understanding.
EDIT =======================
Changed digits.Sort() to digits.Reverse() .
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
modified on Saturday, January 23, 2010 7:52 AM
|
|
|
|
|
you have silently assumed the "integer" would fit in an int, in which case I agree with your code.
For larger variables, you would need long and Math.Pow() would lack the accuracy to get it right in all cases...
So the next homework assignment should be: find the smallest positive long value for which JSOP's code fails. Where is the new article category?
[ADDED]
Shall we call the final Sort a typo?
[/ADDED]
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
modified on Friday, January 22, 2010 5:53 PM
|
|
|
|
|
The OP specified an int, so it's all good.
Besides, this is just showing him how to do it, and isn't meant as an all-encompassing fulling vetted and tested method.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
And here's the pointless math exercise that accounts for negative integers:
int value = int.MinValue;
double i = 1;
double power = 0;
List<int> digits = new List<int>();
while (value != 0)
{
power = Math.Pow(10d, i);
digits.Add((value % (int)power) * ((value < -10) ? -1 : 1));
value = (int)((double)value / power);
}
digits.Reverse();
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
modified on Saturday, January 23, 2010 8:27 AM
|
|
|
|
|
And finally, the pointless math exercise extracting the digits from left to right instead of right to left.
bool isNegative = (value < 0);
List<int> digits = new List<int>();
int value = int.MinValue;
double i = 9;
double power = 0;
while (value != 0)
{
power = Math.Pow(10d, i);
int result = (value - (value % (int)power));
int addResult = (int)((double)result / power) * ((isNegative && (int)i == 9) ? 1 : -1);
digits.Add(addResult);
value -= result;
i--;
}
I hope you can see by my persistence that I've found several similar ways to accomplish the stated goal. This is part of what makes a good programmer.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
hi guys
i want to know how to get output of SSRS(another project in the same solution) to win form using c# language (in the same solution) with visual studio 2008
so any articles or answers can help me
thanks
|
|
|
|
|
I'm sorry everyone but I posted the wrong problem so here it goes again..
When you try to set the rightToLeft property inside the modal form, it vanishes..
this.RightToLeft = RightToLeft.Yes;
It's obvious, it's a bug.. Can you think of any workaround??
Thank you everyone!
|
|
|
|
|
HA HA HA... how many times are you gonna change your question?
anyways, yes, looks like changing this property is sending a dialog result somehow add a this.DialogResult = DialogResult.None; after your this.RightToLeft = RightToLeft.Yes; .
So it becomes:
this.RightToLeft = RightToLeft.Yes;
this.DialogResult = DialogResult.None;
|
|
|
|
|
It works, though I lose focus of the calling form.
|
|
|
|
|
did you do this? if you are loosing focus even after this, i gotta give up because i tested this code.
I am typing it here so check for spell errors.
Original Form:
TestForm form = new TestForm ();
form.ShowDialog();
Modal Form:
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
ChangeRightToLeft();
}
private void ChangeRightToLeft()
{
this.RightToLeft = RightToLeft.Yes;
this.DialogResult = DialogResult.None;
}
}
|
|
|
|
|
Put
this.RightToLeft=RightToLeft.Yes;
in the dialog class' constructor instead of the form_load handler. That will solve the problem and a DialogResult.None will not be returned.
|
|
|
|
|
Works fine in the form's load handler. I don't believe the disappearing has anything to do with changing this property.
/ravi
|
|
|
|
|
I just created a test project. The problem in the original post was verified in my project. Sure enough, the dialog form vanished right after it was created and shown briefly on screen. Maybe it's a bug???
|
|
|
|
|
It's possible. I wasn't able to reproduce it on my VS2008, .NET 3.51, XP SP3 config.
/ravi
|
|
|
|
|
Hey there Rav, long time no see
Yes, it works fine for non-modal forms, but try to call a form using the ShowDialog() and once loaded, try to change its RightToLeft property as follows:
this.RightToLeft = RightToLeft.Yes;
|
|
|
|
|
Hi Muammar, both modal and modeless forms work for me. I set each form's RightToLeft property in the loading handler. If you like, I can send you a .zip ped project but there's nothing special going on here.
[Edit]
I just re-read your post. Let me try to change the property once it's finished loading.
Cheers,
/ravi
|
|
|
|
|
I just confirmed that I can change the RightToLeft property in the OnLoad handler as well as in code (in response to a button click), for both modal and modeless dialogs.
XP SP3, VS2008, .NET 3.5 SP1 (3.5.30729.4926).
/ravi
|
|
|
|
|
|
I guess there's something else that causes it to work for me and not for you guys. I happen to be running IE7 (not that I think that should make a difference, but you never know).
/ravi
|
|
|
|