Kattermelon
Kattermelon

Reputation: 1

Multiplication Table Displaying Incorrectly

I have to write a code to make a 10x10 multiplication table and its display must look like this:

enter image description here

However, I can't figure out how to display my code correctly. Here is my code below. I know I'm close, I'm just not sure what I'm doing wrong.

/*
 * This program displays a multiplication table of the product of every integer from 1 through 10
 * multiplied by every integer from 1 through 10.
 * 
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DisplayMultiplicationTable
{
    class Program
    {
        static void Main(string[] args)
        {
            int value = 10;

            for (int x = 1; x <= value; ++x)

                Console.Write("{0, 4}", x);
            Console.WriteLine();
            Console.WriteLine("_________________________________________");

            for (int x = 1; x <= value; ++x)

                Console.WriteLine("{0, 4}", x);

            for (int row = 1; row <= value; ++row)
            {
                for (int column = 1; column <= value; ++column)
                {

                    Console.Write("{0, 4}", row * column);

                }
                Console.WriteLine();

            }
            Console.ReadLine();
        }

    }
}

Upvotes: 0

Views: 6643

Answers (2)

Parimal Raj
Parimal Raj

Reputation: 20575

Added :

Console.Write("{0, 4}", row);

after start of the row for statement

Fixed Code :

    static void Main(string[] args)
    {
        int value = 10;

        Console.Write("    ");
        for (int x = 1; x <= value; ++x)
            Console.Write("{0, 4}", x);

        Console.WriteLine();
        Console.WriteLine("_____________________________________________");

        for (int row = 1; row <= value; ++row)
        {
            Console.Write("{0, 4}", row);
            for (int column = 1; column <= value; ++column)
            {
                Console.Write("{0, 4}", row * column);
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }

Result :

display

Upvotes: 1

GalacticJello
GalacticJello

Reputation: 11445

    int value = 10;

    // Indent column headers
    Console.Write("{0, 4}", null);

    // Write column headers
    for (int x = 1; x <= value; ++x)
        Console.Write("{0, 4}", x);

    // Write column header seperator
    Console.WriteLine();
    Console.WriteLine("_____________________________________________");

    // Write the table
    for (int row = 1; row <= value; ++row)
    {
        // Write the row header
        Console.Write("{0, 4}", row);

        for (int column = 1; column <= value; ++column)
        {
            // Write the row values
            Console.Write("{0, 4}", row * column);
        }
        // Finish the line
        Console.WriteLine();

    }

enter image description here

Upvotes: 1

Related Questions