Multiplication Tables

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1438 Views - Last Post: 04 October 2012 - 07:32 AM Rate Topic: -----

#1 ankit.mca.aaidu  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 9
  • Joined: 29-September 12

Multiplication Tables

Posted 30 September 2012 - 11:33 AM

Print out the grade school multiplication table upto 12*12.

None
Print out the table in a matrix like fashion, each number formatted to a width of 4 (The numbers are right-aligned and strip out leadeing/trailing spaces on each line). The first 3 line will look like:
e.g.

1   2   3   4   5   6   7   8   9  10  11  12
2   4   6   8  10  12  14  16  18  20  22  24
3   6   9  12  15  18  21  24  27  30  33  36



i am not able to print this column wise can you help please.....

class Program
    {
        static void Main(string[] args)
        {
            int i,j,sum=0;            
            for (i = 1; i <= 12; i++)
            {
                for (j = 1; j <= 10; j++)
                {
                    sum = i * j;
                    Console.WriteLine(sum);
                }
            }
            Console.ReadLine();
        }
    }


this is my code....
please do help me

This post has been edited by tlhIn`toq: 30 September 2012 - 12:35 PM
Reason for edit:: code tags added


Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication Tables

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,689
  • Joined: 05-May 12

Re: Multiplication Tables

Posted 30 September 2012 - 12:19 PM

You need to take some time to dig through the MSDN documentation, and not just rely on Intellisense. You would have found this:
http://msdn.microsof...y/9xdyw6yk.aspx
Was This Post Helpful? 0
  • +
  • -

#3 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 391
  • Joined: 04-January 11

Re: Multiplication Tables

Posted 30 September 2012 - 06:11 PM

The problem with your code is you are only using WriteLine() causing the next number to be written on the next line (like the behavior of 'Enter' key).

I revised your code to the following:

            int intProduct = 0;
            for (int intRow = 1; intRow <= 12; intRow++)
            {
                for (int intColumn = 1; intColumn <= 12; intColumn++)
                {
                    intProduct = intRow * intColumn;
                    Console.Write(intProduct + "\t");
                }
                Console.WriteLine();
            }



Sum is for addition, so I changed it to 'Product' since this is a multiplication table. The "\t" is to add tab.

Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#4 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Multiplication Tables

Posted 30 September 2012 - 09:04 PM

You might also want to look into the MSDN page on Composite Formatting.

Specifically, check how to align text, which is always handy when you want to print a table.
Was This Post Helpful? 0
  • +
  • -

#5 ankit.mca.aaidu  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 9
  • Joined: 29-September 12

Re: Multiplication Tables

Posted 01 October 2012 - 03:55 AM

View PostNovember-06, on 30 September 2012 - 06:11 PM, said:

The problem with your code is you are only using WriteLine() causing the next number to be written on the next line (like the behavior of 'Enter' key).

I revised your code to the following:

            int intProduct = 0;
            for (int intRow = 1; intRow <= 12; intRow++)
            {
                for (int intColumn = 1; intColumn <= 12; intColumn++)
                {
                    intProduct = intRow * intColumn;
                    Console.Write(intProduct + "\t");
                }
                Console.WriteLine();
            }



Sum is for addition, so I changed it to 'Product' since this is a multiplication table. The "\t" is to add tab.

Hope this helps.

hello sir i am thankful for your answer but its not working i want to print tables from 1 to 12 column wise.... each table in each different column
can you have solution for this.
i think i should use 2d array for this it will work??
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4435
  • View blog
  • Posts: 7,707
  • Joined: 08-June 10

Re: Multiplication Tables

Posted 01 October 2012 - 07:12 AM

No, you do not need a 2D array for printing things to the console. Read the link that rfs02 posted, then read this page:

http://blog.stevex.n...ting-in-csharp/

That's a super-handy reference for most commonly used string formatting needs. What you're looking for is called "padding". You want each column to take the same amount of space, even if the content inside is of a different size.
Was This Post Helpful? 2
  • +
  • -

#7 ankit.mca.aaidu  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 9
  • Joined: 29-September 12

Re: Multiplication Tables

Posted 01 October 2012 - 10:36 AM

Hello Sir i tried this and it is working can you please check this out

int i,j,sum=0;            
            for (i = 1; i <= 10; i++)
            {
                for (j = 1; j <= 10; j++)
                {
                    sum = i * j;
                    Console.Write(sum+"\t");
                }
                Console.WriteLine();
            }
            Console.ReadLine();


Was This Post Helpful? -1
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4435
  • View blog
  • Posts: 7,707
  • Joined: 08-June 10

Re: Multiplication Tables

Posted 01 October 2012 - 11:31 AM

You didn't read the links, did you. They explain how to do what you want. We're not going to give you the answer, we're here to help you find it. Both links have the information you need.
Was This Post Helpful? 0
  • +
  • -

#9 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Multiplication Tables

Posted 01 October 2012 - 01:50 PM

View Postankit.mca.aaidu, on 01 October 2012 - 10:36 AM, said:

Hello Sir i tried this and it is working can you please check this out

int i,j,sum=0;            
            for (i = 1; i <= 10; i++)
            {
                for (j = 1; j <= 10; j++)
                {
                    sum = i * j;
                    Console.Write(sum+"\t");
                }
                Console.WriteLine();
            }
            Console.ReadLine();



What you submitted is printing the multiplication tables up to 10x10, your requirement is to go up to 12x12.

Look at your output carefully and look at what's working and what's not. Keep in mind that when printing to the console, once you print 80 characters, the output will overlap to the next line automatically.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,689
  • Joined: 05-May 12

Re: Multiplication Tables

Posted 01 October 2012 - 03:07 PM

View Postrfs02, on 01 October 2012 - 01:50 PM, said:

Keep in mind that when printing to the console, once you print 80 characters, the output will overlap to the next line automatically.


Actually it depends on how you configure your console window. The default setting is 80x25 characters to emulate the old IBM/PC DOS screens, but you can actually set it up to be bigger or smaller. I set mine up to be 120x50 so the compile errors don't wrap around as much and I get to see more errors before they scroll away (forcing me to use the mouse to get back to them).
Was This Post Helpful? 1
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4435
  • View blog
  • Posts: 7,707
  • Joined: 08-June 10

Re: Multiplication Tables

Posted 01 October 2012 - 04:28 PM

Off topic, but do you use csc.exe to compile very often? Or are you talking about using that for other compilers, perhaps C++?
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,689
  • Joined: 05-May 12

Re: Multiplication Tables

Posted 01 October 2012 - 05:32 PM

I create a .csproj, and let msbuild.exe invoke csc.exe for me. I also have the .csproj run xunit.exe to run my unit tests as part of my build. Having the wider and taller console is really useful when the unit tests fail unexpectedly I need to scroll back to see what happened.
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4435
  • View blog
  • Posts: 7,707
  • Joined: 08-June 10

Re: Multiplication Tables

Posted 01 October 2012 - 05:34 PM

Gotcha. We use TFS to handle our builds and unit tests, so we don't see console output, we get handy reports. Also, we use MSTEST. My boss likes to say "happiness is little green check marks".
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,689
  • Joined: 05-May 12

Re: Multiplication Tables

Posted 01 October 2012 - 05:41 PM

Yup, I used to use MSTest as well, but I got fed up with the random hangs and crashes that VS2008 and VS2010 would experience. When I went to the console window + Visual SlickEdit, I only ever fire up VS2010 for debugging, or to look at some the code posted here at DIC. So far VS2012 have been really stable, so I may go back to using VS as my primary environment but the jury is still out.
Was This Post Helpful? 0
  • +
  • -

#15 ankit.mca.aaidu  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 9
  • Joined: 29-September 12

Re: Multiplication Tables

Posted 02 October 2012 - 12:03 AM

View PostCurtis Rutland, on 01 October 2012 - 07:12 AM, said:

No, you do not need a 2D array for printing things to the console. Read the link that rfs02 posted, then read this page:

http://blog.stevex.n...ting-in-csharp/

That's a super-handy reference for most commonly used string formatting needs. What you're looking for is called "padding". You want each column to take the same amount of space, even if the content inside is of a different size.


Hello sir please help me with this question . I am just a beginner in this field i want to do thing and i read the tutorial i am not able to implement it, please if you can do some favor to me
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2