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
using System;
using System.Collections.Generic;
namespace App
{
class Program
{
public static void Main()
{
int i,j;
Console.WriteLine("using - for loop");
for (i = 1, j = 5; i < 6; i++, j--)
{
if (i == 1)
{
Console.Write("{0},{1}", i, j);
}
else
{
Console.Write(",{0},{1}", i, j);
}
}
Console.WriteLine("\n\nusing - while loop");
i = 0;
j = 5;
while (++i < 6)
{
if (i == 1)
{
Console.Write("{0},{1}", i, j);
}
else
{
Console.Write(",{0},{1}", i, j);
}
j--;
}
Console.WriteLine("\n\nusing - do while loop");
i=1;
j = 5;
do
{
if (i == 1)
{
Console.Write("{0},{1}", i, j);
}
else
{
Console.Write(",{0},{1}", i, j);
}
j--;
} while (++i < 6);
Console.ReadLine();
}
}
}
Comments
Leave a comment