is a logic to divide a number randomly? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Does anyone know a logic to divide a number randomly? I need to do this in C#, but it might just be logic here.
Example:
I want to split 10 = 3 + 4 + 1 + 2
I want to split 30 = 2 + 5 + 8 + 4 + 6 + 5

Pseudo Code:
myNumber = 30; //Number to split
int maximumValue = myNumber; //Maximum value we can get with rand()
int totalValue = 0; //Save our total
while(totalValue < myNUmber)
{
newSplitNumber = rand(0, maximumValue); //Use as needed
totalValue+= newSplitNumber; //Update our total
maximumValue = myNumber - totalValue; //Update our maximum on next iteration
}

Related

How to access variables declared inside a loop in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In the below code i have declared an and then assigned it to z. [Please note this is a duplicate]
double an;
for(double z= Amt+Intrest; z>0; z-=MonthlyInstallment)
{
an = z;
}
I want to access the an variable in the below loop.
for (int i = 0; i < Months; i++)
{
dt.Rows.Add(i + 1, MonthlyInstallment, MonthlyIntrest, MonthlyInstallment - MonthlyIntrest,//use the an variable here);
}
You cannot relate the two loops directly, because I assume that those have equal iterations.
A suggestion: You could calculate the value a the loop.
// start with the base value.
double an = Amt + Intrest;
for (int i = 0; i < Months; i++)
{
dt.Rows.Add(i + 1, MonthlyInstallment, MonthlyIntrest, MonthlyInstallment -MonthlyIntrest, an);
// decrease an with the MonthlyInstallment
an = an - MonthlyInstallment;
}

calculating multi squares in one textbox in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm working on windows form application and I have homework to calculate the square of 5 numbers in one textbox and I have to put all 5 numbers in different lines
I expect the results to be like this,
you enter 5 numbers in each line
4
9
16
25
and get them in one single message box saying the results like
2, 3, 4, 5
Pass the Textbox Text into the function and It will Print all the SqureRoot of the Numbers
private void squreRootFinder(string textBox)
{
string[] numbers = textBox.Split('\n');
string data = "";
for (int i = 0; i < numbers.Length; i++)
{
data += Math.Sqrt(Convert.ToInt32(numbers[i])) + " ";
}
MessageBox.Show(data);
}

C# not calculating [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
double cash;
private void btnHesapla_Click(object sender, EventArgs e)
{
int number= Convert.ToInt32(tbadet.Text);
int oneprice= Convert.ToInt32(tbbirim.Text);
cash= number * oneprice;
if (number >= 100)
{
double i = 0;
i =cash- (cash * (5 / 100));
lblcash.Text = cash.ToString() + " $" + (" Discount");
}else
lblcash.Text = cash.ToString() + " $";
}
If the customer is over 100, they will get 5% off
but without a discount calculate
You assign final result to i variable but you use cash variable to show result.
Also, as #Bathsheba said (5 / 100) results to 0 because this operation uses two integers.
Replace this line:
i = cash - (cash * (5 / 100));
by this:
cash -= cash * 5 / 100;
You should assign to cash variable.
5 / 100 is an expression equal to 0 due to integer division.
Removing the parentheses will fix this one, and don't forget to assign to cash, rather than i which you can remove.
cash *= 0.95 is probably clearer.

Autonumber with alternating number and letters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want an auto-numbering class in c# which would generate numbers of 8 digit length in the following format i.e. 1A2B3C4D..one number followed by one letter.Any suggestions??
Pseudocode for generating such string:
String result = "";
for ( int i = 0; i < 8 ; i++)
{
if ( i % 2 == 0)
{
// random(a,b) returns random value between or equal to a-b
result.append(random(0,9).toString());
}
else
{
result.append(random(65,90).toChar()); // Generating a random value between 65-90 (A-Z in ascii)
}
}
Edit:
Or as Sayse suggested:
String result = "";
for (int i = 0; i< 4; i++)
{
result.append(random(0,9).toString());
result.append(random(65-90).toChar());
}

how to multiply the two numbers in datagridview [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to multiply two numbers one by one until last row in datagridview; and add them in a double variable .
They then divided by the total units do.
For example:
mark |units |result
12.00 * 2 = 24.00
20.00 * 3 = 60.00
15.00 * 1 = 15.00
24 + 60 + 15 = 59
59 / 2 + 3 + 1
= 59 /6
= 16.50
so show in a textbox.
for (int i = 0; i < dataGridView1.RowCount ; i++)
{<br>
mark = Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);<br>
unit = Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);<br>
sumunit += unit;<br>
result += ( mark * unit);<br>
}<br>
result /= sumunit;<br>
label2.Text = result.ToString();<br>
}<br>
Do not do it in DataGrid, but do it directly in the data you bind to.
Ad new row were result of computation are stored, and after bind it to DataGrid.

Categories