How to Multiply values like math [closed] - c#

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 8 years ago.
Improve this question
How can I do multiplication in hand form?
by hand form I mean:
Take two numbers as input,
then output in a text box,
the multiplication steps as it would be done by a school student.
so for the inputs 819 and 1358 I wish to output:
8 1 9
1 3 5 8
x_____________
8 1 9 0 0 0
5 4 5 7 0 0
1 0 9 5 0
1 4 5 5 2
+__________________
1 4 7 0 2 0 2
I can of-course get the final answer though the multiplication operation: (a*b)
but that will not let me display the steps.

This should do what you require.
Then modify like following
List<double> lst = new List<double>();
string strInput2 = txtInput2.Text;
for (int i = 0; i < strInput2.Length; i++)
{
double dbl = Convert.ToDouble(txtInput1.Text) * Convert.ToDouble(strInput2[strInput2.Length - (i + 1)].ToString());
string zeros = new String('0', i);
lst.Add(Convert.ToDouble(dbl + zeros));
//richTextBoxResult.Text += lst[i] + Environment.NewLine;
}
for (int i = lst.Count - 1; i >= 0; i--)
{
richTextBoxResult.Text += lst[i] + Environment.NewLine;
}
richTextBoxResult.Text += "________________" + Environment.NewLine;
richTextBoxResult.Text += lst.Sum();

Related

Program to find the product of odd numbers from 1 to n and the sum of the squares of even numbers according to the number n entered from the keyboard [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
I'm new to the programming world and I'm learning C#. I have an algorithm given to me in expression language and I need to do it with C# .NET Framework console application, but I'm not very good at it. Can you help?
Here's the algorithm I'm talking about;
Start
int toplam, carpım, karetoplam
toplam=0
carpım=1
Read N
6.Cycle I=1, N, 1
if(N%2==1)
toplam=toplam+I
carpım=carpım*I
else
karetoplam=karetoplam+(I*I)
if over
cycle over
Cw toplam
Cw carpım
Cw karetoplam
Done
I only understood the title of your post not the text ;)
Code is untested. Could be optimized to 1 for-loop in 1 method.
Product of oddnumber from 1 to n?
long GetProductsOfOddNumbers(int n) {
var product = 1L;
for (var i = 1; i <= n; i++) {
if (i % 2 == 0) continue;
product *= i;
}
return product;
}
Sum of squares of even numbers from 1 to n?
long GetSumOfSquaresOfEvenNumbers(int n) {
var sum = 0L;
for (var i = 1; i <= n; i++) {
if (i % 2 != 0) continue;
sum += (i * i);
}
return sum ;
}

How to make output : 1 1 2 6 3 11 4 16 5 21 [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
How to make output : 1 1 2 6 3 11 4 16 5 21. when i input start value = 1, and end value = 5
my code :
Console.Write("input start value : ");
start = int.Parse(Console.ReadLine());
Console.Write("input end value : ");
end = int.Parse(Console.ReadLine());
Console.WriteLine("");
for (int i = start; i <= end; i++)
{
Console.WriteLine(i);
for (int j = i; j <= end; j++)
{
int z = 1;
if (start != j)
{
z++;
Console.WriteLine((j * j) + z);
}
else
{
Console.WriteLine(start + " this j start value");
}
}
}
So it's not entirely clear to me if the 5 is used both as an end value for the 1,2,3,4,5 as well as a difference value for the 1,6,11,16,21 but I'll assume yes. Here's an algorithm for you to implement (this looks like homework, so think of this as a tip - you'll get more out of doing the coding yourself but this is how you should approach any coding exercise: write out the algorithm in the language you think in then translate it to c#)
ask the user for a start value and convert to int
ask the user for an end value and convert to int
work out a variable called n which is end minus start
make a for loop starting at x = 0, running while x is less than or equal to n ; incrementing x by 1
print out startValue plus x
print out startValue plus (endValue times x)
loop around
For a start and end of 1 and 5, the loop runs from 0 to 4. The first time the loop runs, x is 0, startValue is 1, so a 1+0 and a 1+(5*0) are printed - both 1. This continues up to the end value where x is 4, 4+1 is printed - which is 5 - and 1+(4*5) is printed - which is 21
As #dxiv has posted in the comments, the pattern for the set of numbers is to combine 1,2,3,4,5 and 1,6,11,16,21. The pattern that I see is that the gap between the second set of numbers is equal to the ending number.
We can define a function which generates these numbers:
IEnumerable<int> GetNumbers(int start, int end)
{
for (int number = start; number <= end; number++)
{
yield return number;
yield return start + ((number - 1) * end);
}
}
And can output the results like:
int start = 1;
int end = 5;
Console.WriteLine(string.Join(' ', GetNumbers(start, end)));
Output
1 1 2 6 3 11 4 16 5 21

C# Read int numbers to array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
using System;
using System.IO;
namespace Sudoku
{
class Game
{
private int[,] puzzle = new int[9, 9];
public void saveToFile()
{
StreamWriter str = new StreamWriter("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
str.Write(puzzle[i, j] + " ");
}
str.Write("\t\n");
}
str.Close();
}
public void readFromFile()
{
clear();
StreamReader str = new StreamReader("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
puzzle[i, j] = Convert.ToInt32(str.Read());
}
}
str.Close();
}
}
}
I can not download the data from the file.
Saving works fine and has a view of the txt file:
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 5 8 9 7 2 1 4
8 9 7 2 1 4 3 6 5
5 3 1 6 4 2 9 7 8
6 4 2 9 7 8 5 3 1
9 7 8 5 3 1 6 4 2
How it all written in my array 9x9 skipping all the gaps that would be all the data is written correctly?
Instead of using str.Read() which would require you to read single characters (or a buffer that you specified), try using str.Readline() to read a single line for each iteration of i.
public void readFromFile()
{
StreamReader str = new StreamReader("SUDOKU.txt");
for (int i = 0; i < 9; ++i)
{
string[] lineNumbers = str.ReadLine().Split(' ');
for (int j = 0; j < 9; ++j)
{
puzzle[i, j] = Convert.ToInt32(lineNumbers[j]);
}
}
str.Close();
}
This reads a single line at a time (each iteration of i), splits the line into lineNumbers by separating the current line by space characters. Each number on the current line can then be accessed by lineNumbers[j] within your inner loop (each iteration of j).

divide the money value to zero? [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 have a problem to divide the money value to "0".
i build accounting software, with a shrinking asset.
example, i have a value 1000000.
i would divide it into 3.
1000000 / 3 = 333333,33333333333333333333333
the question is, i would to do this:
step 1 :1000000 - 333333,33333333333333333333333 =
666666,66666666666666666666667
step 2 :666666,66666666666666666666667 -
333333,33333333333333333333333 = 333333,33333333333333333333334
step 3 :333333,33333333333333333333334 -
333333,33333333333333333333333 =
0,00000000000000000000001
example code
decimal value = 100;
int divider = 3;
decimal x = value / divider;
for (int i = 0; i < divider; i++)
{
value = value - x;
}
the result, value = 0,000000000000000000000000001 .
yeah, that is a problem.
that a money value, i would divide to zero.
I've tried using math method like a "math.floor" at each step (on loop).
the result value is -2.
i know i can using "math.floor" method for the final result. but i don't, because i want to do step to step (on loop.)
what should i do?
thanks
You should consider multiplying your 1/3 by 1, 2, or 3 instead of subtracting the previous number. Something like this should work:
var originalAmount = 1000000d;
var thirdOfOriginal = originalAmount / 3;
Console.WriteLine("Original number ... {0}", originalAmount);
Console.WriteLine("Original - 1/3 .... {0}", (originalAmount - (thirdOfOriginal * 1)));
Console.WriteLine("Original - 2/3 .... {0}", (originalAmount - (thirdOfOriginal * 2)));
Console.WriteLine("Original - 3/3 .... {0}", (originalAmount - (thirdOfOriginal * 3)));
// Output
// Original number ... 1000000
// Original - 1/3 .... 666666.666666667
// Original - 2/3 .... 333333.333333333
// Original - 3/3 .... 0
This could be written more generically as:
private static List<decimal> GetAmounts(decimal originalNumber, int numberOfDivisions)
{
var amounts = new List<decimal>();
if (numberOfDivisions > 0)
{
var fraction = originalNumber / numberOfDivisions;
for (int i = 1; i <= numberOfDivisions; i++)
{
amounts.Add(originalNumber - (fraction * i));
}
}
return amounts;
}

Algo reversing 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 8 years ago.
Improve this question
I have some algorithm written in c#:
int num3 = 260005;
string text = this.textBox1.Text;
int length = text.Length - 4;
int num5 = ((Convert.ToInt32(text.Substring(4, length)) - 0x7d1) / 2) - 0x7d1;
if (num3 == num5)
{
do somthing!!!
}
Can somebody explain how to get correct input for textBox1? I try some math but i stuck.
I see now. I was confused with 4 character that are trow away.
This code will match what you are looking for... This is because you are throwing away the leading FOUR characters of the text box and then doing a simple equation of ((x - 2001) / 2) - 2001 = 260005 therefore x = 526013:
int num3 = 260005;
string text = "XXXX526013";
int length = text.Length - 4;
int num5 = ((Convert.ToInt32(text.Substring(4, length)) - 0x7d1) / 2) - 0x7d1;
if (num3 == num5)
{
Console.WriteLine("Match!");
}
else
{
Console.WriteLine("No Match! {0}", num5);
}
You can simply reverse your algorithm:
string text = "asdf" + (((num3 + 0x7d1)*2) + 0x7d1);

Categories

Resources