Saturday, July 4, 2015

// // 7 comments

Java Program To Print Table

Java Program To Print Table


The program below is the answer to Liang's Introduction To Java Programming (9th Edition) Chapter 1 Exercise 1.4.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 1.4 - Print A Table
 *
 */ 

public class Ex01_04 {

 public static void main(String[] args) {

  System.out.println("a       a^2       a^3");
  System.out.println("1       1         1");
  System.out.println("2       4         8");
  System.out.println("3       9         27");
  System.out.println("4       16        64");

 }
}


Click here to see other solutions to Introduction to Java Programming.

7 comments:

  1. I highly appreciate this post. It’s hard to be good from the bad sometimes, but I think you’ve nailed it! would you mind updating your blog with more information?

    ReplyDelete
    Replies
    1. Hi Malik, thanks for the comment. I try to update the blog as often as possible. Just add the site to your bookmarks so you can easily revisit it.

      Delete
  2. oh man, I think it must use for loop.
    thanks :d.

    ReplyDelete
    Replies
    1. public static void main(String[] args) {
      for (int i = 0; i < 3; i++) {
      System.out.println("a^"+(i+1));
      for (int j = 0; j < 4; j++) {
      if(i==0) System.out.println(j+1);
      if(i==1) System.out.println((j+1)*(j+1));
      if(i==2) System.out.println((j+1)*(j+1)*(j+1));
      }

      }

      Delete
    2. Hello @CanhDuc KCO, I know you can use loops to solve the exercise but the aim is to use techniques that have been taught up to that exercise chapter in the textbook (in this case Exercise 1.4 of the 9th Edition).

      P.S. Though your code is correct, it doesn't produce the required output. Your code prints the table on a single vertical line. A better code can be found here: Java Program to Print Table Using Loop.

      Thanks for your contribution.

      Delete
  3. Can l solve it like that:
    for(int i 1;i<=4;i++)
    S.o.p(math.pow(i,1);
    S.o.p(math.pow(i,2);
    S.o.p(math.pow(i,3),

    ReplyDelete
  4. Can l solve it like that:
    for(int i 1;i<=4;i++)
    S.o.p(math.pow(i,1);
    S.o.p(math.pow(i,2);
    S.o.p(math.pow(i,3),

    ReplyDelete


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.