Jump to content

C# Homework help.

780blzit

Hellooo, my homework is to write the multiplication table 1-12 using two loops. 

Which wasn't a problem, but making it look nice is. 

 

What I've got so far is:

            //Färg            Console.BackgroundColor = ConsoleColor.Black;            Console.ForegroundColor = ConsoleColor.Gray;            Console.Clear();            //Loop&Utskrivning            int m = 0;            for (int i = 1; i <= 12; i++)            {                for (int t = 1; t <= 12; t++)                {                     m = i * t                    Console.WriteLine(m);                }            }

What I want, is for the multiplication table for every number 1-2 to be in their own column.

Basically: 1 2 3 4 5 

                2 4 6 8 10 

 

and so on. 

 

Thank you! 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Few code quality things

* Remove all the unnecessary whitespace, i.e. lines 10, 13, 16, 18 and 19

* The variable m isn't necessary at all, just pass i * t into the print function. If m was required (for example if you were using the result of i * t more than once and don't want to calculate it twice) then you should declare m inside the loop rather than putting the declaration outside the loop.

 

For making the output prettier, rather than using WriteLine you want to use a StringBuilder and append to it the number followed by a space, so you start with an empty string "", append "1 " to it giving "1 " then add "2 " to that giving "1 2 " and so on then print it at the end of the inner loop with a new line character.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×