apply the for looping statement
Write a program that calculates and produces these two columns sequence numbers using the three looping statements:
Sequence nos. Squared
1 1
2 4
3 9
4 16
5 25
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
const int n = 5;
int i;
Console.WriteLine("1st Solution – using for loop");
for (i = 1; i <= n; i++)
{
int square = i * i;
Console.WriteLine("{0} {1}", i, square);
}
Console.WriteLine("2nd Solution – using while loop");
i = 0;
while (++i <= n)
{
int square = i * i;
Console.WriteLine("{0} {1}", i, square);
}
Console.WriteLine("3rd Solution - using do while loop");
i = 1;
do
{
int square = i * i;
Console.WriteLine("{0} {1}", i, square);
}
while (++i <= n);
return;
}
}
}
Comments
Leave a comment