Using the while ( ), do - while (), and for ( ) loops, generate the following screen
output:
Sample output
Series generated using while ( )
1 1 1 2 4 8 3 9 27
Series generated using do-while ( )
1 2 3 1 4 9 1 8 27
Series generated using for ( )
3 9 27 8 4 2 1 1 1
using System;
namespace Series_with_for_and_while
{
internal class Program
{
public static void Main(string[] args)
{
// Series generated using while ( )
// 1 1 1 2 4 8 3 9 27
int i = 1;
while (i != 4)
{
Console.Write(Math.Pow(i,1) + " ");
Console.Write(Math.Pow(i,2) + " ");
Console.Write(Math.Pow(i,3) + " ");
i++;
}
i = 1;
Console.WriteLine("\n");
// Series generated using do-while ( )
// 1 2 3 1 4 9 1 8 27
do
{
Console.Write(Math.Pow(1,i) + " ");
Console.Write(Math.Pow(2,i) + " ");
Console.Write(Math.Pow(3,i) + " ");
i++;
} while (i != 4);
Console.WriteLine("\n");
// Series generated using for ( )
// 3 9 27 8 4 2 1 1 1
for (int j = 3; j > 0; j--)
{
if (j % 2 != 0)
{
Console.Write(Math.Pow(j, j-j+1) + " ");
Console.Write(Math.Pow(j, j-j+2) + " ");
Console.Write(Math.Pow(j, j-j+3) + " ");
}
else
{
Console.Write(Math.Pow(j, 3) + " ");
Console.Write(Math.Pow(j, 2) + " ");
Console.Write(Math.Pow(j, 1) + " ");
}
}
}
}
}
Comments
Leave a comment