Pyramid Output:
1
212
32123
4321234
543212345
........................
........................
The Code Explained by below:
//Ex. 4321234 - Here "432" - first part and "1"-Middle part then "234" - Second part.
//StringBuilder is the bsest option for printing the sequnce numbers in same row /line.
var sbObj = new StringBuilder();
int n = 5; // ToDO-Read the input from Console
for (int i = 1; i <= n; i++)
{
if (i == 1)
sbObj.AppendLine(i.ToString());
else
{
int k = i; //Assign i value to k because we can not reduce or increment i. why means it will affect main for loop.
int e = 0; // This property is going to help out the right outer section like 1234 (ex: 4321234)
for (int j = 0; j < i; j++) // This loop helps out the printing numbers like 32123. Main use is looping purpose only.
//we can not do anything with this j element.
{
if (j > 0 && k != i || e > 0) //1. J > 0 denotes avoid the k's intial or first value.
//1.2 Because we dont do inrement or decrement with this k's intial value. why means it is incremented value (i) already by main loop.
//1.3 so we need to print the actual value with the help of 'else' part.
//2. k != i denotes Here k is always not equal to i to be execute the below code
//3. j > 0 && k != 1 denotes both condition is not allow the first values from j and k for the subsequent looping purpose.
//3.1 Because the subsequent loop increases the j value but not k value. so both condition is must.
//4. e > 0 denotes the second part of execution i.e., k--. The (j>0 && k!=i) is used to first part of exectuion i.e., k++.
{
if (k == 1) //1. when k is reduced to 1 (that is middle value of all sequences) then we will start increment the value of k for the second part purpose.
{
sbObj.Append(k.ToString()); //Bind the k value before increment. Because middle value is always same, that is 1.
k += 1; //when k ==1 that is middle value then we need to increment for the second part purpose like 123
j = 0; //1.This line helps to avoid extra loop. Because here we set j=0 then the inner for loop knows we do loop again and start with 0.
//1.2 J++ increases the value automatically. Then only the loop start with next of zero (0).
//1.3 This behaviour helps to print second part exact numbers. Ex. 123 means it prints only 123 instead of 1234.
e += 1; //This line helps out about the if condition to print the second part.
}
else //This part help to print remianing numbers rather then k's intial value upto end.
{
if (k != i && e == 0) //Here we also check Both condition. Because it is used to avoid second part printing numbers i.e, k++.
//It prints only first part numbers and avoid intial values from k and e properties.
//Why we avoid means this is decrement part. so we move next decrement numbers like 4321.
{
sbObj.Append(k.ToString()); //Append the k value before increment becuase once it is incrementd then it shows wrong sequence.
k -= 1; //This is denotes first part numbers upto 1 like 321. This is decrement order
}
else //This section helps to print the second part numbers i.e.., k++.
{
sbObj.Append(k.ToString());//Append the k value before increment.
k += 1;//Thsi is used to second part numbers upto end value like 123.
}
}
}
else//This section is used to print the k's initial values always.
{
if (e == 0) //Confirmes the below code is only for first part numbers.
{
sbObj.Append("\n" + k.ToString()); //Append k's initial value (k=i) to the string builder and start with new line.
//Because all intial values should start with new line.
k -= 1;//This is help to print the first part decrement numbers like 321
}
}
}
}
}
Console.WriteLine(sbObj); //Print the all values from main string builder object. Here we use "\n" so it will print one by one line.
Console.ReadLine();
The Actual Code is as follows:
class PyramidClass
{
public static void Maiin(string[] args)
{
Console.WirteLine("Please entter the integer value");
int n= Convert.ToInt32(Console.ReadLine());
var sbObj = new StringBuilder();
for (int i = 1; i <= n; i++)
{
if (i == 1)
sbObj.AppendLine(i.ToString());
else
{
int k = i;
int e = 0;
for (int j = 0; j < i; j++)
{
if (j > 0 && k != i || e > 0)
{
if (k == 1)
{
sbObj.Append(k.ToString());
k += 1;
j = 0;
e += 1;
}
else
{
if (k != i && e == 0)
{
sbObj.Append(k.ToString());
k -= 1;
}
else
{
sbObj.Append(k.ToString());
k += 1;
}
}
}
else
{
if (e == 0)
{
sbObj.Append("\n" + k.ToString());
k -= 1;
}
}
}
}
}
Console.WriteLine(sbObj);
Console.ReadLine();
}
}
1
212
32123
4321234
543212345
........................
........................
The Code Explained by below:
//Ex. 4321234 - Here "432" - first part and "1"-Middle part then "234" - Second part.
//StringBuilder is the bsest option for printing the sequnce numbers in same row /line.
var sbObj = new StringBuilder();
int n = 5; // ToDO-Read the input from Console
for (int i = 1; i <= n; i++)
{
if (i == 1)
sbObj.AppendLine(i.ToString());
else
{
int k = i; //Assign i value to k because we can not reduce or increment i. why means it will affect main for loop.
int e = 0; // This property is going to help out the right outer section like 1234 (ex: 4321234)
for (int j = 0; j < i; j++) // This loop helps out the printing numbers like 32123. Main use is looping purpose only.
//we can not do anything with this j element.
{
if (j > 0 && k != i || e > 0) //1. J > 0 denotes avoid the k's intial or first value.
//1.2 Because we dont do inrement or decrement with this k's intial value. why means it is incremented value (i) already by main loop.
//1.3 so we need to print the actual value with the help of 'else' part.
//2. k != i denotes Here k is always not equal to i to be execute the below code
//3. j > 0 && k != 1 denotes both condition is not allow the first values from j and k for the subsequent looping purpose.
//3.1 Because the subsequent loop increases the j value but not k value. so both condition is must.
//4. e > 0 denotes the second part of execution i.e., k--. The (j>0 && k!=i) is used to first part of exectuion i.e., k++.
{
if (k == 1) //1. when k is reduced to 1 (that is middle value of all sequences) then we will start increment the value of k for the second part purpose.
{
sbObj.Append(k.ToString()); //Bind the k value before increment. Because middle value is always same, that is 1.
k += 1; //when k ==1 that is middle value then we need to increment for the second part purpose like 123
j = 0; //1.This line helps to avoid extra loop. Because here we set j=0 then the inner for loop knows we do loop again and start with 0.
//1.2 J++ increases the value automatically. Then only the loop start with next of zero (0).
//1.3 This behaviour helps to print second part exact numbers. Ex. 123 means it prints only 123 instead of 1234.
e += 1; //This line helps out about the if condition to print the second part.
}
else //This part help to print remianing numbers rather then k's intial value upto end.
{
if (k != i && e == 0) //Here we also check Both condition. Because it is used to avoid second part printing numbers i.e, k++.
//It prints only first part numbers and avoid intial values from k and e properties.
//Why we avoid means this is decrement part. so we move next decrement numbers like 4321.
{
sbObj.Append(k.ToString()); //Append the k value before increment becuase once it is incrementd then it shows wrong sequence.
k -= 1; //This is denotes first part numbers upto 1 like 321. This is decrement order
}
else //This section helps to print the second part numbers i.e.., k++.
{
sbObj.Append(k.ToString());//Append the k value before increment.
k += 1;//Thsi is used to second part numbers upto end value like 123.
}
}
}
else//This section is used to print the k's initial values always.
{
if (e == 0) //Confirmes the below code is only for first part numbers.
{
sbObj.Append("\n" + k.ToString()); //Append k's initial value (k=i) to the string builder and start with new line.
//Because all intial values should start with new line.
k -= 1;//This is help to print the first part decrement numbers like 321
}
}
}
}
}
Console.WriteLine(sbObj); //Print the all values from main string builder object. Here we use "\n" so it will print one by one line.
Console.ReadLine();
The Actual Code is as follows:
class PyramidClass
{
public static void Maiin(string[] args)
{
Console.WirteLine("Please entter the integer value");
int n= Convert.ToInt32(Console.ReadLine());
var sbObj = new StringBuilder();
for (int i = 1; i <= n; i++)
{
if (i == 1)
sbObj.AppendLine(i.ToString());
else
{
int k = i;
int e = 0;
for (int j = 0; j < i; j++)
{
if (j > 0 && k != i || e > 0)
{
if (k == 1)
{
sbObj.Append(k.ToString());
k += 1;
j = 0;
e += 1;
}
else
{
if (k != i && e == 0)
{
sbObj.Append(k.ToString());
k -= 1;
}
else
{
sbObj.Append(k.ToString());
k += 1;
}
}
}
else
{
if (e == 0)
{
sbObj.Append("\n" + k.ToString());
k -= 1;
}
}
}
}
}
Console.WriteLine(sbObj);
Console.ReadLine();
}
}
The Output is below:
Comments
Post a Comment