Assignment#1
Step1:
As you can see step1, in this step first
count number of lines (in our case there are 9 lines). Means that there will be
an outer loop which executes 9 times.
Step2:
Secondly see closely from which number
loop has started, yes it start from 1-9.
Means that inner loop will print 1 in
first iteration, 2 1 in second iteration, 3 2 1 in third iteration and so on.
Code:
for (int i = 1; i < 10;
i++)
{
for (int j = i; j > 0;
j--)
{
Console.Write(j);
}
Console.WriteLine();
}
Assignment#2
First of all
break design in 4 steps
At first step
count number of lines for outer loop.
At second
step break the design in two pieces because we cannot use a single loop for
print
1 2 1
Or
1 2 3 2 1
In this way I
divide it in two pieces.
At third step
count number of space before first number (there are 8 space) after each iteration these number of spaces will be decrease as you can see in figure above.
At fourth
step it tells you that there is another design as we have seen in our first
assignment.
So,
Outer loop is
use for first step.
First loop inside
outer loop is use for printing white space.
Second loop
inside outer loop is use for printing the design that is between step 1 and
step 2.
Third loop
inside outer loop is use for printing the design that is at step 4.
Code:
for (int i = 1; i < 10;
i++)
{
for (int j = i; j < 9; j++)
{
Console.Write(" ");
}
for (int k = 1; k <=i; k++)
{
Console.Write(k);
}
for (int l = i-1; l >0; l--)
{
Console.Write(l);
}
Console.WriteLine();
}
Assignment#3
As you can
see step 1 is similar to our previous assignment 2.
At step 2 we
calculate number of line for outer loop (which are 8).
At step 3, I break
design in two pieces. As we have done in previous example.
At step 4 we
calculate number of spaces(initially it will be 1 then 2 and so on till 8 spaces).
In this
example we will use two outer loops, one for step 1, second for step 2, step 3
step 4 and step 5.
Code:
for (int i = 1; i < 10;
i++)
{
for (int j = i; j < 9;
j++)
{
Console.Write(" ");
}
for (int k = 1; k <=i;
k++)
{
Console.Write(k);
}
for (int l = i-1; l >0; l--)
{
Console.Write(l);
}
Console.WriteLine();
}
for (int i = 1; i < 9;
i++)
{
for (int j = i; j > 0;
j--)
{
Console.Write(" ");
}
for (int k = 1; k <= 9-i; k++)
{
Console.Write(k);
}
for (int l = 8-i; l >0; l--)
{
Console.Write(l);
}
Console.WriteLine();
}





No comments:
Post a Comment