Write a program which produces the given sequences numbers (in alternate arrangement and reverse order of the problem no. 2) using the three
looping statements: 5, 1, 4, 2, 3, 3, 2, 4, 1, 5,
1st Solution – using for loop
2nd Solution – using while loop
3rd Solution-‐ using do while loop
internal class Program
{
static void Main(string[] args)
{
string array = GetReverseArrayUsingFor(("5, 1, 4, 2, 3").Split(',').ToArray());
Console.WriteLine($"Use for: {array}");
array = GetReverseArrayUsingWhile(("5, 1, 4, 2, 3").Split(',').ToArray());
Console.WriteLine($"Use while: {array}");
array = GetReverseArrayUsingDoWhile(("5, 1, 4, 2, 3").Split(',').ToArray());
Console.WriteLine($"Use do while: {array}");
Console.ReadKey();
}
static string GetReverseArrayUsingFor(string[] array)
{
string result = "";
for (int i = array.Count()-1; i >= 0; i--)
{
result += array[i]+",";
}
return result;
}
static string GetReverseArrayUsingWhile(string[] array)
{
string result = "";
int i = array.Count()-1;
while (i>=0)
{
result += array[i] + ",";
i--;
}
return result;
}
static string GetReverseArrayUsingDoWhile(string[] array)
{
string result = "";
int i = array.Count() - 1;
do
{
result += array[i] + ",";
i--;
} while (i>=0);
return result;
}
}
Comments
Leave a comment