Print multiplication table of C

Design explanation

It can be seen from the figure that:

1. We need to print out nine lines;

2. The maximum number of columns in each row is equal to the number of rows;

code implementation

        public void Display1()
        {
            Console.WriteLine("Multiplication table:");
            for (int i = 1; i <= 9; i++) // Number of rows in multiplication table of cyclic output
            {
                for (int j = 1; j <= i; j++) // Number of columns in multiplication table of cyclic output
                {
                    Console.Write(i + "*" + j + "=" + i * j + "  ");

                }
                Console.WriteLine();

            }
            Console.WriteLine();
        }    

 

Design sketch

If you have obsessive-compulsive disorder (for example, I will find that the third and fourth lines are not aligned with the following lines;

Looking at the renderings, we can see that only one more space needs to be printed after the results of 3 * 2 and 4 * 2, and the multiplication tables will be aligned;

code implementation

        public void Display2()
        {
            Console.WriteLine("Multiplication table:");
            for (int i = 1; i <= 9; i++) // Left value of cyclic output multiplication table
            {
                    for (int j = 1; j <= i; j++) // Cycle output multiply table right value
                    {
                    if ((i == 3 || i == 4) && j == 2) // Sort multiplication tables in order
                    {
                        Console.Write(i + "*" + j + "=" + i * j + "   ");
                    }
                    else {
                        Console.Write(i + "*" + j + "=" + i * j + "  ");
                    }
                        
                    }
                    Console.WriteLine();
                
            }
        }    

Design sketch

 

Complete code

using System;

namespace multiplicationTableApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var multiplication = new Multiplication();
            multiplication.Display1();
            multiplication.Display2();

            Console.ReadLine();
        }
    }

    class Multiplication
    {
        public void Display1()
        {
            Console.WriteLine();
            Console.WriteLine("Misaligned multiplication tables:");
            for (int i = 1; i <= 9; i++) // Number of rows in multiplication table of cyclic output
            {
                for (int j = 1; j <= i; j++) // Number of columns in multiplication table of cyclic output
                {
                    Console.Write(i + "*" + j + "=" + i * j + "  ");

                }
                Console.WriteLine();

            }
            Console.WriteLine("\n");
        }

        public void Display2()
        {
            Console.WriteLine("Aligned multiplication tables:");
            for (int i = 1; i <= 9; i++) // Left value of cyclic output multiplication table
            {
                    for (int j = 1; j <= i; j++) // Cycle output multiply table right value
                    {
                    if ((i == 3 || i == 4) && j == 2) // Sort multiplication tables in order
                    {
                        Console.Write(i + "*" + j + "=" + i * j + "   ");
                    }
                    else {
                        Console.Write(i + "*" + j + "=" + i * j + "  ");
                    }
                        
                    }
                    Console.WriteLine();
                
            }
        }
    }
}

Tags: C#

Posted on Sat, 04 Apr 2020 15:27:40 -0700 by atticus