apply the following looping statement : for loop, while loop, and do while loop
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
Expert's answer
using System;
using System.Collections.Generic;
namespace App
{
class Program
{
static void Main(string[] args)
{
//for loop
Console.WriteLine("for loop");
int number = 1;
for (number = 1; number <= 5; number++)
{
Console.WriteLine("{0} {1}", number, (number * number));
}
//while loop
Console.WriteLine("while loop");
number = 1;
while (number <= 5)
{
Console.WriteLine("{0} {1}", number, (number * number));
number++;
}
//do while loop
Console.WriteLine("do while loop");
number = 1;
do
{
Console.WriteLine("{0} {1}", number, (number * number));
number++;
} while (number <= 5);
Console.ReadLine();
}
}
}
Comments
Leave a comment