How to divide a number into multiple parts so that the resulting sum is equal to the input?
for ex :
input from textbox :

first input is(txt_val1) :5
second input is(txt_val2) :3

output :
3+2=5
2+3=5
4+1=5
1+4=5
or any other combinations like these.
both input are dynamic always.

## All 9 Replies

For instance if you allow complex numbers, (2 + 42i) + (3 - 42i) would be a solution.

Why do you input two values?

Something like this will work:

``````    Dim input As Integer = Val(TextBox1.Text)
Dim output As New List(Of String)
If input > 0 Then
For i = 1 To input - 1
For j = 1 To input - 1
If i + j = input Then
End If
Next
Next
For Each s As String In output
Debug.WriteLine(s.Split(",")(0) + " + " + s.Split(",")(1) + " = " + input.ToString.Trim)
Next
End If
``````

Or if you want to keep the parts separate something like this would work:

``````Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim input As Integer = Val(TextBox1.Text)
Dim output As New List(Of EquationParts)
If input > 0 Then
For i = 1 To input - 1
For j = 1 To input - 1
If i + j = input Then
Dim e As New EquationParts
e.Firstpart = i
e.SecondPart = j
End If
Next
Next
For Each e As EquationParts In output
Debug.WriteLine(e.Firstpart.ToString.Trim + " + " + e.SecondPart.ToString.Trim + " = " + input.ToString.Trim)
Next
End If
End Sub
End Class
Public Class EquationParts
Public Firstpart As Integer = 0
Public SecondPart As Integer = 0
End Class
``````

@tinstaafl: That is a fine piece of Visual Basic code on the C# forum. I think an "oops" would be appropriate here.

Yep oops. let's try it again:

``````        Int32 input = 0;
List<string> output = new List<string>();
bool GoodInput = int.TryParse(textBox1.Text, out input);
if (GoodInput && input >1)
{
for (int i = 1; i < input; i++)
{
for (int j = 1; j < input; j++)
{
if (i + j == input)
{
}
}
}
foreach (string s in output)
{
textBox2.AppendText(s.Split(',') + " + " + s.Split(',') + " = " + input.ToString().Trim());
}
}
``````

And the code for saving the parts:

``````namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Int32 input = 0;
List<EquationParts> output = new List<EquationParts>();
textBox2.Clear();
bool GoodInput = int.TryParse(textBox1.Text, out input);
if (GoodInput && input >1)
{
for (int i = 1; i < input; i++)
{
for (int j = 1; j < input; j++)
{
if (i + j == input)
{
EquationParts eq = new EquationParts();
eq.FirstPart = i;
eq.SecondPart = j;
}
}
}
foreach (EquationParts eq in output)
{
textBox2.AppendText(eq.FirstPart.ToString().Trim() + " + " + eq.SecondPart.ToString().Trim() + " = " + input.ToString().Trim() + "\r\n");
}
}

}
}
public class EquationParts
{
public int FirstPart{get;set;}
public int SecondPart{get;set;}
}
}
``````

hi..

but my problem is :

i have two textboxs
txt_val1 and txt_val2
now i want to divide the numbers like : val1/val2 and the addition equals to val1
like:
5/3
then it should divide 5 three times. but the output addition must be 5
like:
input : 5/3
output :1+3+1 =5
input :7/5
output : 1+2+2+1+1=7 // it divide the values five times and addition of all number is 7
some think like this.

thank you once again.

Please explain what you mean by divide.
Example: 5/3 could be 1 or 1.666666666666666666666666666666666...

This is starting to sound more and more like a homework assignment. Perhaps you should be showing what effort you're putting into this.

I got the solution.

here is code(console) :

``````using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int temp;
int number1 = 0;
int sum = 0;
Console.WriteLine("Please enter a number : ");
Console.WriteLine("Please enter a number to divide: ");
if (number1 % number2 == 0)
{
temp = number1 / number2;
int div = temp;
Console.WriteLine(temp);
for (int i = 1; i < number2; i++)
{
temp = temp + div;
Console.WriteLine(temp);
}
}
else
{
for (int i = 0; i < number2; i++)
{
temp = number1 / number2;
sum = sum + temp;
if (sum >= number1)
{
break;
}
number1 = number1 + 1;
Console.WriteLine(temp);
temp = 0;
}

}