Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Programming a power table

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Programming a power table

    I'm a beginner at java and one of my assignments is to make a table like a times table but instead of a times table it's a power table
    example:

    1 2 3 4 5
    2 4 8 16 25
    3 9 27 81 243
    4 16 64 256 1024
    5 25 125 625 3125

    I thought I had the right idea here but it doesn't seem to work the way i want it.

    {
            int rows = 12;
            int columns = 12;
            for(int i=1;i<=rows;i++)
            {
                for(int j=1;j<=columns;j++)
                {
                    System.out.printf("%5s",i^j);
                }
                System.out.print("\n");
            }
        }

    how do I create a power table? I wish I could figure it out on my own and not ask others for help but I just can't seem to be able to figure it out.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Programming a power table

    The carot '^' operator is a bitwise XOR operator. Its more of an advanced topic, the results of which are not powers of. You might want to use Math.pow, or alternatively a loop where the value is multiplied iteratively.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Programming a power table

    Quote Originally Posted by copeg View Post
    The carot '^' operator is a bitwise XOR operator. Its more of an advanced topic, the results of which are not powers of. You might want to use Math.pow, or alternatively a loop where the value is multiplied iteratively.
    I've replaced the "i^j" with Math.pow(i,j).

    In the run window I get all of the values in double as Math.pow returns the numbers as type double. How can I convert it to int? I do it by type casting right? How do I do that again?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Programming a power table

    int x = (int) answer;

Similar Threads

  1. Custom Table using AbstractTableModel
    By dumb_terminal in forum AWT / Java Swing
    Replies: 11
    Last Post: November 16th, 2010, 10:02 AM
  2. table comparison
    By awecode in forum JDBC & Databases
    Replies: 2
    Last Post: October 12th, 2010, 09:37 AM
  3. how to insert the value to table in form
    By palani in forum AWT / Java Swing
    Replies: 4
    Last Post: September 3rd, 2010, 12:23 PM
  4. Replies: 2
    Last Post: July 8th, 2009, 06:35 AM
  5. Getting table height using JSTL
    By jsnx7 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 19th, 2009, 12:03 PM