Write a program that produces the given sequence nos. (in alternate arrangement) using
the three looping statements: 1, 5, 2, 4, 3,3, 4, 2, 5, 1,
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 = UsingFor(("1, 5, 2, 4, 3").Split(',').ToArray());
Console.WriteLine($"Use for: {array}");
array = UsingWhile(("1, 5, 2, 4, 3").Split(',').ToArray());
Console.WriteLine($"Use while: {array}");
array = UsingDoWhile(("1, 5, 2, 4, 3").Split(',').ToArray());
Console.WriteLine($"Use do while: {array}");
Console.ReadKey();
}
static string UsingFor(string[] array)
{
string result = "";
for (int i = array.Count()-1; i >= 0; i--)
{
result += array[i]+",";
}
return result;
}
static string UsingWhile(string[] array)
{
string result = "";
int i = array.Count()-1;
while (i>=0)
{
result += array[i] + ",";
i--;
}
return result;
}
static string UsingDoWhile(string[] array)
{
string result = "";
int i = array.Count() - 1;
do
{
result += array[i] + ",";
i--;
} while (i>=0);
return result;
}
}
Comments
Leave a comment