Reusable formulas and starter patterns
This page is a short memory aid. It is here so you do not have to search the whole site for the same formula again and again.
Use it when you remember the task type but not the exact line of code or formula.
Console starter
One clean input -> process -> output pattern.
Forms event flow
A short reminder of what goes in the Designer and what goes in Form1.cs.
Browser starter
A small file set for HTML, CSS, and JavaScript.
Three formulas that repeat often
These are the formulas people look up again and again on this site.
| Task | Formula or rule | Reminder |
|---|---|---|
| Rectangle perimeter | 2 * (width + height) | Use both sides twice. |
| Rectangle area | width * height | Only one multiplication. |
| Quadratic discriminant | b * b - 4 * a * c | Check a = 0 first. |
| Driving range | fuel / consumption * 100 | Consumption is liters per 100 km. |
| Leap year | (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 | 100 and 400 are the easy places to make mistakes. |
Average vs. median
Both are middle-type values, but they do not mean the same thing.
Quick rule
- Average: add all values and divide by the count.
- Median: sort the values and take the middle one.
- If there is an even count, average the two middle values.
Small examples
- 1, 2, 100 -> average 34.33, median 2
- 1, 2, 3, 4 -> median 2.5
Typical event flow
When a WinForms example feels confusing, the problem is usually file placement, not the calculation itself.
Normal order
- Place the controls on the form in the Designer.
- Set the Name of each control.
- Open
Form1.cs. - Attach the click event or double-click the button.
- Read values, validate them, calculate, then write the result to a label.
Button-click pattern
- Read input from TextBox
- Use TryParse if the input should be a number
- Calculate
- Write result into Label or MessageBox
Resistor band reminder
A short table is often enough for common three-band resistor tasks.
| Color | Digit | Multiplier |
|---|---|---|
| Black | 0 | 1 |
| Brown | 1 | 10 |
| Red | 2 | 100 |
| Orange | 3 | 1,000 |
| Yellow | 4 | 10,000 |
| Green | 5 | 100,000 |
| Blue | 6 | 1,000,000 |
| Violet | 7 | - |
One clean browser task template
Use this when a teacher says: make a page with HTML, CSS, and a small button action.
Build order
- Create the three files in one folder.
- Paste the HTML first.
- Link the CSS and JavaScript files correctly.
- Open the HTML file and test the button.
Good check
- If the style does not load, check the CSS file name and
href. - If the button does nothing, check the JavaScript file name and
src.
FILE: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Starter page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Starter page</h1>
<button id="action-button" type="button">Click me</button>
<script src="script.js"></script>
</body>
</html>
FILE: style.css
body {
font-family: Arial, sans-serif;
margin: 24px;
}
button {
padding: 10px 14px;
font: inherit;
}
FILE: script.js
document.getElementById("action-button").addEventListener("click", () => {
alert("It works.");
});
One clean console starter
This is the smallest useful console pattern: input, process, output.
Use it like this
- Replace the input line with the value you need.
- Replace the process line with your formula or condition.
- Keep the output line clear and short.
Good check
- If the task needs numbers, change the input line to
TryParse. - If the task needs a loop, wrap the input-process-output part in a loop.
FILE: Program.cs
Console.Write("Input: ");
string input = Console.ReadLine() ?? "";
string result = input.ToUpper();
Console.WriteLine($"Result: {result}");