C# How to use a while loop with a percentage from a TextBox
I have been searching of a way to do this little exercise from my programming book, but I'm not able to think how to do this.
Here is what I have so far:
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = Convert.ToDouble(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
while (numOrg <= 10)
{
lstDisplay.Items.Add(numOrg + " " + convertToDecimal );
numOrg++;
}
}
For the values I am using: numOrg is 2 and numberToConvert is 30%.
The expected output should be: 2.6000, 3.3800, 4.3940, etc.
I just want to add an increase to my number each day. I definitely over complicated this simple task, but I've put 7 hours into this already and I'm not getting anywhere so I'm hoping someone can help me see this problem a little more clearly. Thanks in advance guys!
c#
|
show 2 more comments
I have been searching of a way to do this little exercise from my programming book, but I'm not able to think how to do this.
Here is what I have so far:
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = Convert.ToDouble(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
while (numOrg <= 10)
{
lstDisplay.Items.Add(numOrg + " " + convertToDecimal );
numOrg++;
}
}
For the values I am using: numOrg is 2 and numberToConvert is 30%.
The expected output should be: 2.6000, 3.3800, 4.3940, etc.
I just want to add an increase to my number each day. I definitely over complicated this simple task, but I've put 7 hours into this already and I'm not getting anywhere so I'm hoping someone can help me see this problem a little more clearly. Thanks in advance guys!
c#
1
Do you want to calculate percentage ? Please clarify your problem clearly..
– Yogesh Patel
Nov 23 '18 at 12:32
I want to add 30% increase to my number each day. For example, the first day is 2 so, 2.6... the next day is 3 so the old value (2.6 + 30%)
– Chakra Bracelet
Nov 23 '18 at 12:34
At the moment you're only dividing the entered percentage by 100 and adding it tolstDisplaymultiple times. You need to do some calculation with that value (I assume you have some basic knowledge of maths...)
– ArieKanarie
Nov 23 '18 at 12:37
Suppose you enter 100 innumOrgand 10 inDailyIncrease. Are you expecting increament ofnumOrgby 10 % should add inlstDisplay? So inlstDisplay110 should be added. Is this your requirement?
– Yogesh Patel
Nov 23 '18 at 12:39
Yes, exactly, and then the next number would be 101 with the 10% DailyIncrease so on and so forth
– Chakra Bracelet
Nov 23 '18 at 12:43
|
show 2 more comments
I have been searching of a way to do this little exercise from my programming book, but I'm not able to think how to do this.
Here is what I have so far:
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = Convert.ToDouble(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
while (numOrg <= 10)
{
lstDisplay.Items.Add(numOrg + " " + convertToDecimal );
numOrg++;
}
}
For the values I am using: numOrg is 2 and numberToConvert is 30%.
The expected output should be: 2.6000, 3.3800, 4.3940, etc.
I just want to add an increase to my number each day. I definitely over complicated this simple task, but I've put 7 hours into this already and I'm not getting anywhere so I'm hoping someone can help me see this problem a little more clearly. Thanks in advance guys!
c#
I have been searching of a way to do this little exercise from my programming book, but I'm not able to think how to do this.
Here is what I have so far:
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = Convert.ToDouble(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
while (numOrg <= 10)
{
lstDisplay.Items.Add(numOrg + " " + convertToDecimal );
numOrg++;
}
}
For the values I am using: numOrg is 2 and numberToConvert is 30%.
The expected output should be: 2.6000, 3.3800, 4.3940, etc.
I just want to add an increase to my number each day. I definitely over complicated this simple task, but I've put 7 hours into this already and I'm not getting anywhere so I'm hoping someone can help me see this problem a little more clearly. Thanks in advance guys!
c#
c#
edited Nov 23 '18 at 14:33
Callum Watkins
1,66611532
1,66611532
asked Nov 23 '18 at 12:27
Chakra BraceletChakra Bracelet
4
4
1
Do you want to calculate percentage ? Please clarify your problem clearly..
– Yogesh Patel
Nov 23 '18 at 12:32
I want to add 30% increase to my number each day. For example, the first day is 2 so, 2.6... the next day is 3 so the old value (2.6 + 30%)
– Chakra Bracelet
Nov 23 '18 at 12:34
At the moment you're only dividing the entered percentage by 100 and adding it tolstDisplaymultiple times. You need to do some calculation with that value (I assume you have some basic knowledge of maths...)
– ArieKanarie
Nov 23 '18 at 12:37
Suppose you enter 100 innumOrgand 10 inDailyIncrease. Are you expecting increament ofnumOrgby 10 % should add inlstDisplay? So inlstDisplay110 should be added. Is this your requirement?
– Yogesh Patel
Nov 23 '18 at 12:39
Yes, exactly, and then the next number would be 101 with the 10% DailyIncrease so on and so forth
– Chakra Bracelet
Nov 23 '18 at 12:43
|
show 2 more comments
1
Do you want to calculate percentage ? Please clarify your problem clearly..
– Yogesh Patel
Nov 23 '18 at 12:32
I want to add 30% increase to my number each day. For example, the first day is 2 so, 2.6... the next day is 3 so the old value (2.6 + 30%)
– Chakra Bracelet
Nov 23 '18 at 12:34
At the moment you're only dividing the entered percentage by 100 and adding it tolstDisplaymultiple times. You need to do some calculation with that value (I assume you have some basic knowledge of maths...)
– ArieKanarie
Nov 23 '18 at 12:37
Suppose you enter 100 innumOrgand 10 inDailyIncrease. Are you expecting increament ofnumOrgby 10 % should add inlstDisplay? So inlstDisplay110 should be added. Is this your requirement?
– Yogesh Patel
Nov 23 '18 at 12:39
Yes, exactly, and then the next number would be 101 with the 10% DailyIncrease so on and so forth
– Chakra Bracelet
Nov 23 '18 at 12:43
1
1
Do you want to calculate percentage ? Please clarify your problem clearly..
– Yogesh Patel
Nov 23 '18 at 12:32
Do you want to calculate percentage ? Please clarify your problem clearly..
– Yogesh Patel
Nov 23 '18 at 12:32
I want to add 30% increase to my number each day. For example, the first day is 2 so, 2.6... the next day is 3 so the old value (2.6 + 30%)
– Chakra Bracelet
Nov 23 '18 at 12:34
I want to add 30% increase to my number each day. For example, the first day is 2 so, 2.6... the next day is 3 so the old value (2.6 + 30%)
– Chakra Bracelet
Nov 23 '18 at 12:34
At the moment you're only dividing the entered percentage by 100 and adding it to
lstDisplay multiple times. You need to do some calculation with that value (I assume you have some basic knowledge of maths...)– ArieKanarie
Nov 23 '18 at 12:37
At the moment you're only dividing the entered percentage by 100 and adding it to
lstDisplay multiple times. You need to do some calculation with that value (I assume you have some basic knowledge of maths...)– ArieKanarie
Nov 23 '18 at 12:37
Suppose you enter 100 in
numOrg and 10 in DailyIncrease. Are you expecting increament of numOrg by 10 % should add in lstDisplay? So in lstDisplay110 should be added. Is this your requirement?– Yogesh Patel
Nov 23 '18 at 12:39
Suppose you enter 100 in
numOrg and 10 in DailyIncrease. Are you expecting increament of numOrg by 10 % should add in lstDisplay? So in lstDisplay110 should be added. Is this your requirement?– Yogesh Patel
Nov 23 '18 at 12:39
Yes, exactly, and then the next number would be 101 with the 10% DailyIncrease so on and so forth
– Chakra Bracelet
Nov 23 '18 at 12:43
Yes, exactly, and then the next number would be 101 with the 10% DailyIncrease so on and so forth
– Chakra Bracelet
Nov 23 '18 at 12:43
|
show 2 more comments
2 Answers
2
active
oldest
votes
Please check this code:
static void Main(string args)
{
var numOrg = 2;
var percentage = 0.3;
var result = (double)numOrg;
while (numOrg <= 10)
{
result += percentage * result;
Console.WriteLine($"{numOrg}: {result}");
numOrg++;
}
Console.ReadKey();
}
It generates expected result:
2: 2,6
3: 3,38
4: 4,394
5: 5,7122
6: 7,42586
7: 9,653618
8: 12,5497034
9: 16,31461442
10: 21,208998746
I dont't know what exact logic is behind your code. Maybe you should loop from 1 to stop value and use numOrg only as value for calculations.
Hi there, sorry for the code example I provided. This was after my second day of trying to get the result. Your example made this so clear for me to understand. Now I feel silly that I wasn't able to figure this out lol. Thanks a lot for your time!
– Chakra Bracelet
Nov 23 '18 at 12:57
add a comment |
try this
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = double.Parse(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
var result = double.Parse(txtNumOrg.Text);
while (numOrg <= 10)
{
result += convertToDecimal * result;
lstDisplay.Items.Add(result);
numOrg++;
}
}

add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446737%2fc-sharp-how-to-use-a-while-loop-with-a-percentage-from-a-textbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please check this code:
static void Main(string args)
{
var numOrg = 2;
var percentage = 0.3;
var result = (double)numOrg;
while (numOrg <= 10)
{
result += percentage * result;
Console.WriteLine($"{numOrg}: {result}");
numOrg++;
}
Console.ReadKey();
}
It generates expected result:
2: 2,6
3: 3,38
4: 4,394
5: 5,7122
6: 7,42586
7: 9,653618
8: 12,5497034
9: 16,31461442
10: 21,208998746
I dont't know what exact logic is behind your code. Maybe you should loop from 1 to stop value and use numOrg only as value for calculations.
Hi there, sorry for the code example I provided. This was after my second day of trying to get the result. Your example made this so clear for me to understand. Now I feel silly that I wasn't able to figure this out lol. Thanks a lot for your time!
– Chakra Bracelet
Nov 23 '18 at 12:57
add a comment |
Please check this code:
static void Main(string args)
{
var numOrg = 2;
var percentage = 0.3;
var result = (double)numOrg;
while (numOrg <= 10)
{
result += percentage * result;
Console.WriteLine($"{numOrg}: {result}");
numOrg++;
}
Console.ReadKey();
}
It generates expected result:
2: 2,6
3: 3,38
4: 4,394
5: 5,7122
6: 7,42586
7: 9,653618
8: 12,5497034
9: 16,31461442
10: 21,208998746
I dont't know what exact logic is behind your code. Maybe you should loop from 1 to stop value and use numOrg only as value for calculations.
Hi there, sorry for the code example I provided. This was after my second day of trying to get the result. Your example made this so clear for me to understand. Now I feel silly that I wasn't able to figure this out lol. Thanks a lot for your time!
– Chakra Bracelet
Nov 23 '18 at 12:57
add a comment |
Please check this code:
static void Main(string args)
{
var numOrg = 2;
var percentage = 0.3;
var result = (double)numOrg;
while (numOrg <= 10)
{
result += percentage * result;
Console.WriteLine($"{numOrg}: {result}");
numOrg++;
}
Console.ReadKey();
}
It generates expected result:
2: 2,6
3: 3,38
4: 4,394
5: 5,7122
6: 7,42586
7: 9,653618
8: 12,5497034
9: 16,31461442
10: 21,208998746
I dont't know what exact logic is behind your code. Maybe you should loop from 1 to stop value and use numOrg only as value for calculations.
Please check this code:
static void Main(string args)
{
var numOrg = 2;
var percentage = 0.3;
var result = (double)numOrg;
while (numOrg <= 10)
{
result += percentage * result;
Console.WriteLine($"{numOrg}: {result}");
numOrg++;
}
Console.ReadKey();
}
It generates expected result:
2: 2,6
3: 3,38
4: 4,394
5: 5,7122
6: 7,42586
7: 9,653618
8: 12,5497034
9: 16,31461442
10: 21,208998746
I dont't know what exact logic is behind your code. Maybe you should loop from 1 to stop value and use numOrg only as value for calculations.
answered Nov 23 '18 at 12:47
Tomasz CyborowskiTomasz Cyborowski
22616
22616
Hi there, sorry for the code example I provided. This was after my second day of trying to get the result. Your example made this so clear for me to understand. Now I feel silly that I wasn't able to figure this out lol. Thanks a lot for your time!
– Chakra Bracelet
Nov 23 '18 at 12:57
add a comment |
Hi there, sorry for the code example I provided. This was after my second day of trying to get the result. Your example made this so clear for me to understand. Now I feel silly that I wasn't able to figure this out lol. Thanks a lot for your time!
– Chakra Bracelet
Nov 23 '18 at 12:57
Hi there, sorry for the code example I provided. This was after my second day of trying to get the result. Your example made this so clear for me to understand. Now I feel silly that I wasn't able to figure this out lol. Thanks a lot for your time!
– Chakra Bracelet
Nov 23 '18 at 12:57
Hi there, sorry for the code example I provided. This was after my second day of trying to get the result. Your example made this so clear for me to understand. Now I feel silly that I wasn't able to figure this out lol. Thanks a lot for your time!
– Chakra Bracelet
Nov 23 '18 at 12:57
add a comment |
try this
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = double.Parse(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
var result = double.Parse(txtNumOrg.Text);
while (numOrg <= 10)
{
result += convertToDecimal * result;
lstDisplay.Items.Add(result);
numOrg++;
}
}

add a comment |
try this
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = double.Parse(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
var result = double.Parse(txtNumOrg.Text);
while (numOrg <= 10)
{
result += convertToDecimal * result;
lstDisplay.Items.Add(result);
numOrg++;
}
}

add a comment |
try this
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = double.Parse(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
var result = double.Parse(txtNumOrg.Text);
while (numOrg <= 10)
{
result += convertToDecimal * result;
lstDisplay.Items.Add(result);
numOrg++;
}
}

try this
private void btnDisplay_Click(object sender, EventArgs e)
{
int numOrg = Convert.ToInt32(txtNumOrg.Text);
double numberToConvert = double.Parse(txtDailyIncrease.Text); // percent
double convertToDecimal = (numberToConvert / 100);
var result = double.Parse(txtNumOrg.Text);
while (numOrg <= 10)
{
result += convertToDecimal * result;
lstDisplay.Items.Add(result);
numOrg++;
}
}

edited Nov 23 '18 at 15:49
answered Nov 23 '18 at 15:42
Dhanushka DayawanshaDhanushka Dayawansha
351110
351110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446737%2fc-sharp-how-to-use-a-while-loop-with-a-percentage-from-a-textbox%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Do you want to calculate percentage ? Please clarify your problem clearly..
– Yogesh Patel
Nov 23 '18 at 12:32
I want to add 30% increase to my number each day. For example, the first day is 2 so, 2.6... the next day is 3 so the old value (2.6 + 30%)
– Chakra Bracelet
Nov 23 '18 at 12:34
At the moment you're only dividing the entered percentage by 100 and adding it to
lstDisplaymultiple times. You need to do some calculation with that value (I assume you have some basic knowledge of maths...)– ArieKanarie
Nov 23 '18 at 12:37
Suppose you enter 100 in
numOrgand 10 inDailyIncrease. Are you expecting increament ofnumOrgby 10 % should add inlstDisplay? So inlstDisplay110 should be added. Is this your requirement?– Yogesh Patel
Nov 23 '18 at 12:39
Yes, exactly, and then the next number would be 101 with the 10% DailyIncrease so on and so forth
– Chakra Bracelet
Nov 23 '18 at 12:43