Answer to Question #271859 in C# for chi

Question #271859

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


1
Expert's answer
2021-11-27T02:33:09-0500
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;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS