February 6, 2014

Diamond Loop Using C# and VB .Net

Hello eveyone! In this post I'm going to show you how to create diamond loop.
At the end of this post we will be able to see this output:
     *
    * *
   * * *
  * * * *
 * * * * *
  * * * *
   * * *
    * *
     *

But before we can achieve our output we need to split in to two parts: Pyramid and the Inverted Pyramid.
Pyramid:
     *
    * *
   * * *
  * * * *

Inverted Pyramid:
 * * * * *
  * * * *
   * * *
    * *
     *

Now that we have split the diamond into two parts, we can now set the Pyramid as our first task and the Inverted Pyramid as our second task.

Here's what will do on the first task. First, is to create a loop that will create a new line. Second, to create a loop that will help in forming the pyramid and lastly a loop that will create the body of the pyramid.

Code below for the first step:
for (i = 0; i <= 5; i++)
{
}

For the second step:
for (int j = i; j <= 5; j++)
{
strResult += " ";
}

For the last step:
for (int k = 1; k <= i; k++)
{
strResult += " *";
}

We now then compile the codes.

// The loop created on the first step
for (i = 0; i <= 5; i++)
{
// The loop created on the second step
// which will create the spaces that gives form
// to the pyramid
for (int j = i; j <= 5; j++)
{
strResult += " ";
}
// this will be the loop that will create the body
// of the pyramid
for (int k = 1; k <= i; k++)
{
strResult += " *";
}
// this will create a new line
// to move to the next line of the pyramid
strResult += System.Environment.NewLine;
}


And we are done with the first Pyramid. We will now proceed to the reverse pyramid or the second part of the pyramid. The process of creating the reverse pyramid is almost the same with the pyramid the only difference is that we switch the looping statement we created in the last step with the second step and changing the value in strResult.

This is how it looks like:

for (l = 0; l <= 5; l++)
{
for (int k =1; k <= l; k++)
{
strResult += " ";
}
for (int j = l; j <= 5; j++)
{
strResult += " *";
}
strResult += System.Environment.NewLine;
}

That's it! All we need to do now is to display on a TextBox with a Multiline enabled so that we can see the output.

Download SourceCode: Diamond Loop in CSharp and VB .Net

February 1, 2014

Create New Project in Microsoft Visual Studio 2012

Before we proceed to the source codes we need to create a project or solution, right? If you already know how to create a project/solution file then you may skip this post, if not then let's proceed.

To start with, I will be using Visual Studio 2012 in this blog. This is also the same with the lower versions of Visual Studio however the big difference is the GUI (Graphical User Interface).

Now we will try to create a C# Windows Forms Application. Just follow the simple steps below to get it done.

Steps in creating C# Windows Forms Application:
1. Open Microsoft Visual Studio
2. Click New Project
3. Select Visual C# under Templates Section
4. Click Windows
5. Click Windows Forms Application
6. Enter any name you want for your project in the Name text box
7. Optional: Change the location of your project on where you want to save
8. On the solution you can leave as "Create new solution"
9. In the solution name, you can leave it just like that but it can also be changed to whatever Solution name you like.
10. Click OK and you're done!

Here are the graphical representation of the steps below:

For Step 1 and 2:

Visual Studio Start Page

For the rest of the steps:

Creating a new project/solution

This will be the output when you are done creating a project.



If you want to create other project files or for example you want to create a VB .Net project. You will simply follow these steps but you will only going to select the Visual Basic under Templates Section.

So that's it! Simple right? And there's no need to practice on this one.

Welcome to NoSweatCoding

As for my first post in this blog, I would like to introduce myself, I'm Ernie A. and I've been a Software Developer with more than 1 year of experience using C# and VB .Net.

In this blog you will be expecting lots of working source codes in .Net Framework both C# and VB .Net. So, I hope you would enjoy and learn something.

Once again, Welcome to NoSweatCoding! Have a nice day!