Wednesday, July 1, 2015

// // 3 comments

Java Program To Print Table Of Squares And Cubes

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 2 Exercise 2.31.

Question: Using only the programming techniques you learned in this chapter, write an application that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format, as shown below. [Note: This program does not require any input from the user.]

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 2.31 - Table Of Squares And Cubes
 * This Program Calculates And Prints A Table Of Squares And Cubes Of The Numbers From 0 to * 10
 *
 */ 


public class Ex02_31 {
    public static void main (String [] args) {

 System.out.println ("Number   Square   Cube");
        System.out.printf ("%d        %d        %d\n", 0, (0 * 0), (0 * 0 * 0));
        System.out.printf ("%d        %d        %d\n", 1, (1 * 1), (1 * 1 * 1));
        System.out.printf ("%d        %d        %d\n", 2, (2 * 2), (2 * 2 * 2));
        System.out.printf ("%d        %d        %d\n", 3, (3 * 3), (3 * 3 * 3));
        System.out.printf ("%d        %d       %d\n", 4, (4 * 4), (4 * 4 * 4));
        System.out.printf ("%d        %d       %d\n", 5, (5 * 5), (5 * 5 * 5));
        System.out.printf ("%d        %d       %d\n", 6, (6 * 6), (6 * 6 * 6));
        System.out.printf ("%d        %d       %d\n", 7, (7 * 7), (7 * 7 * 7));
        System.out.printf ("%d        %d       %d\n", 8, (8 * 8), (8 * 8 * 8));
        System.out.printf ("%d        %d       %d\n", 9, (9 * 9), (9 * 9 * 9));
        System.out.printf ("%d       %d      %d\n", 10, (10 * 10), (10 * 10 * 10));
    }
}


3 comments:

  1. ehm you can do this exercise with one line of code, this stuff it's too much

    ReplyDelete
    Replies
    1. @Alex-Kun Yes, you can but the question states that "only programming techniques learnt in the chapter(chapter 2) of the book" should be used.

      Delete
  2. how can i do this exact problem but with a while loop in it?
    in simple form.

    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.