how to write application that calculate squares and cube of the number from 0 to 10 and output in table. the program does not require any input from user?

Dani AI

Generated

A few clarifications and a compact, no-input example that fits the original requirement (numbers 0 through 10).

's array approach is fine, but note an off-by-one: covering 0..10 requires an array of length 11 (indices 0..10). Using length 10 while looping 0..10 will cause an ArrayIndexOutOfBoundsException. 's BufferedReader example demonstrates computing a single value from input, which is unnecessary here since the task explicitly requires no user input.

A concise Java program (prints an aligned table for 0..10, computes values as it prints):

import java.util.stream.IntStream;

public class SquaresAndCubes {
    public static void main(String[] args) {
        System.out.printf("%3s %8s %10s%n", "n", "square", "cube");
        IntStream.rangeClosed(0, 10)
                 .forEach(n -> {
                     int sq = Math.multiplyExact(n, n);
                     int cu = Math.multiplyExact(sq, n);
                     System.out.printf("%3d %8d %10d%n", n, sq, cu);
                 });
    }
}

Notes: use new int[11] when storing results for 0..10. For larger ranges switch to long or BigInteger to avoid overflow (int squares overflow above 46340; int cubes above 1290). System.out.printf keeps columns readable without extra memory.

Two arrays length 10. In one you will put the squares and at the other the cubes.
Use a for loop from 0 to 10. Calculate inside the loop theresults and put them in the array:

square[i] = i*i;
cube[i] = i*i*i;

You can do this without Arrays as well.
Since this is solved, I will just post full code

Use buffer reader
----

import java.io.*;
public class Square {
	  public static void main(String[] args){

	    int s=0;
	           try{
	        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
	        System.out.println("Enter Length of a Side  : ");
	        s = Integer.parseInt(br1.readLine());
	        double area = s*s;
	        System.out.println("Area of Square : "+area);
	        double  volume =s*s*s ;
	        System.out.println("Volume of Cube : "+volume);
	      }
	      catch(Exception e){
	        System.out.println("Error : "+e);
	      }        
	  }
	}

EDIT: /facepalm, didnt see he said no input. ARG

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.