C# not calculating [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 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.

Related

is a logic to divide a number randomly? [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 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
}

I just do not understand how this code is able to calculate factorial of the given numbers [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 6 years ago.
Improve this question
using System;
namespace CalculatorApplication
{
class NumberManipulator
{
public int factorial(int num)
{
/* local variable declaration */
int result;
if (num == 1)
{
return 1;
}
else
{
result = factorial(num - 1) * num;
return result;
}
}
static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
//calling the factorial method
Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6));
Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
Console.ReadLine();
}
}
}
so i know it has to do with the function being a recursion but how is the code able to know when to keep multiplying the integers less than 6 in order to find the factorial. Also how does the code know when to stop multiplying when it reaches 6. If this was in a loop i could fully understand how this would be possible but with no loop i just do not understand.
I'm still in the tutorial of learning c#, if you can please try to keep it as simple as possible.
Any help would be greatly appreciated.
Let's step through factorial(6).
What's factorial(6)? factorial(5) * 6.
What's factorial(5)? factorial(4) * 5.
What's factorial(4)? factorial(3) * 4.
What's factorial(3)? factorial(2) * 3.
What's factorial(2)? factorial(1) * 2.
What's factorial(1)? 1.
You then just go backward from there and you'll arrive at your answer:
factorial(6) = 1 * 2 * 3 * 4 * 5 * 6.

C# logic (solution needed in coding) [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 6 years ago.
Improve this question
I want to use as loop like : Example
i putted in textbox 123 and 300 in my formula.
1/2*(300+123/300) = 150.205 >> answer
i want to loop this Example i got answer 150.205 next formula should be look like this
1/2*(150.205+123/150.205) = 75.512
the answer of this equation i want to put in next formula by loop.
i have written code but i don't know how to use it via loop
My code.
double value = (0.5 * (300 + 123 / 300));
=======================================
For End loop
When condition match like this
1/2*(11.091+123/11.091) = 11.091
Meaning Answer and input where i m putting 300 will be same i want to break loop
**Example** I want to do this without using square root function in c#
like simple if i want a root of 9 it will be 3 so it will be like this .
i choosen 1 Because 9 is one value so i choosen 1
1/2*(1+9/1) = 5.000
1/2*(5+9/5) = 3.400
1/2*(3.4+9/3.4) = 3.024
1/2*(3.024+9/3.024) = 3.000
1/2*(3+9/3) = 3.000
see you will get same value in one point always
The only tricky thing here is a comparison with tolerance, since because of round up errors you can well never meet
answer == value
condition. The implementation could be
double answer = 300.0;
double tolerance = 1e-10;
while (true) {
// based on previous answer we compute next one
double value = 0.5 * (answer + 123.0 / answer);
//TODO: you can print out steps here, if you want something like this
//Console.WriteLine(value);
// check convergence with tolerance
if (Math.Abs(answer - value) <= tolerance) {
answer = value;
break;
}
// next answer (value) becomes the previous one (answer)
answer = value;
}
// 11.0905365064094
Console.Write(answer);
The actual answer (prove it) is just a square root:
// 11.09053650640941716205160010261...
Console.Write(Math.Sqrt(123));
Real life implementation (if my boss wants me to implement it):
public static double NewtonEstimation(Func<double, double> function,
double tolerance = 1e-10,
double guess = 1.0) {
if (null == function)
throw new ArgumentNullException("function");
else if (tolerance < 0)
throw new ArgumentOutOfRangeException("tolerance", "tolerance must not be negative");
while (true) {
double value = function(guess);
if (Math.Abs(value - guess) <= tolerance)
return value;
guess = value;
}
}
...
// 11.0905365064094
Console.Write(NewtonEstimation(x => 0.5 * (x + 123 / x)));

Write an expression that looks for a given integer if its third digit (right to left) is 7 [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 7 years ago.
Improve this question
The question is clear however the code is ambiguous . can anyone explain how this code work specifically this segment int thirdDigit = (number / 100) % 10;
using System;
class ThirdDigit
{
static void Main()
{
Console.Write("Please enter a number: ");
int number = int.Parse(Console.ReadLine());
int thirdDigit = (number / 100) % 10;
if (thirdDigit == 7)
{
Console.WriteLine("The third digits IS seven!");
}
else
{
Console.WriteLine("The third digit IS NOT seven.");
}
}
}
Let's think about the operations applied with a concrete example, 8821 and 7740.
8821 / 100 == 88 // integer division
88 % 10 == 8 // and is not 7
7740 / 100 == 77 // integer division
77 % 10 == 7 // and is 7
On the modulo step, the result is only == 7 if the remainder is 7.
Conceptually it's simpler to think of this as:
Strip the one's and ten's places
then strip all but the one's place
Test the result
First integer division is used (number/100), this returns a number with the third digit of the original number as its last digit. 12345/100 = 123. Secondly the modulus operator is used (% 10) this returns the reminder when dividing with ten, 123 % 10 = 3.

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