Creating Math Worksheets Generator

The StringBuilder is a very versatile class in the .Net Framework. In this tutorial, its Append() method is used to generate a set of quadratic equations to build a worksheet generator.

The numbers are generated with the aid of Random() object. Since StringBuilder class can be used to embed HTML tags inside Append() method, the randomly-generated can easily be arranged in a table. The table has been formatted using Bootstrap table class.

 

 

Amazing StringBuilder Class of ASP.Net

The StringBuilder class exposes several methods. In this tutorial, we focus on significant methods, along with the Random object. The following animation shows the methods and properties available:

asp.net stringbuilder

 

The following code shows how it can be used to generate 10 random quadratic equations easily, by using StringBuilder.Append() method. Please click the button below the code.

StringBuilder sb = new StringBuilder(); // declare StringBuilder object
Random rnd = new Random(); // declare Random object sb.Append("");
for (int i = 1; i <= 10; i++)
{
int a = rnd.Next(2, 10);
int b = rnd.Next(5, 15);
double c = rnd.NextDouble();
if (c < 0.8) { a = -a; }
int d = a + b;
if (d == 0) { d = -2; }
if (d == 1 || d == -1) { d = -5; }
int f = a * b;
if (d > 0 && f > 0) { sb.Append(""); }
else if (d > 0 && f < 0) { sb.Append(""); }
else if (d < 0 && f > 0) {sb.Append(""); }
else { sb.Append(""); }
}
sb.Append("
NumberQuadratic Equation
" + i + "" + "x2 + " + d + "x + " + f + " = 0
" + i + "" + "x2 + " + d + "x " + f + " = 0
" + i + "" + "x2 " + d + "x + " + f + " = 0
" + i + "" + "x2 " + d + "x " + f + " = 0
");
Literal1.Text = sb.ToString();

 

 

How many questions? - maximum:40   

 

 

You can generate any number of questions - maximum 40 - with the above code. Each click on the button create the questions randomly. It shows the awesome power of the StringBuilder class of ASP.Net.

Generating a comma delimited list

The following code generates the first ten Roman numerals as a comma-delimited list:

StringBuilder sbLine = new StringBuilder();
sbLine.Append("The Roman Numerals up to Ten:");
for (int j = 1; j <= 10; j++)
{
if (j == 10) { sbLine.Append("&#" + (8543 + j)).Append(" "); }
else { sbLine.Append("&#" + (8543 + j)).Append(" , "); }
}
Literal2.Text = sbLine.ToString();

Please note the role played by " , " part in organizing the heading and the numbers. Please click on the button.

 

 

First Hundred Numbers in 10 Lines

The code shows how the numbers are arranged in ten lines with the use of HTML <br> in it. Pleae click on the button.

StringBuilder sbLines = new StringBuilder();
sbLines.Append("The First 100 Numbers:
");
for (int j = 1; j <= 100; j++)
{
if (j == 10 || j == 20 || j == 30 || j == 40 || j == 50 || j == 60 || j == 70 || j == 80 || j == 90) { sbLines.Append(j);sbLines.AppendLine("
"); }
else if (j < 100) { sbLines.Append(j).Append(" , "); }
else { sbLines.Append(j); }
}
Literal3.Text = sbLines.ToString();

 

 

 

 

Recommended Reading

Amazon Best Seller

 

Advertisements

Amazon Best Seller

Advertisement