0

"Write a program that prints a table of the values of (ln n), n, n *(ln n), n2, n3, and 2n for n = 16, 32, 64, ..., 2048. Use tabs ('\t' characters) to line up columns."

This was the assignment. However while using \t, the output is improperly spaced. The spacing between the values are not even and some values are shifted to the right. How can I fix this?

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

      for (int n = 16; n<2049; n*=2){
          System.out.println(Math.log(n) +"\t"+ n + "\t"+ (Math.log(n)*n)+"\t"+Math.pow(n,2)+"\t"+Math.pow(n,3)+"\t"+Math.pow(2,n)+"\t");
      }
  }
}
BipoN
  • 93
  • 8

2 Answers2

1

Okay, the tabs are fine, but remember, they're probably tabbed to 8 spaces, and this code, when I run it, prints your doubles to more digits than that. The linked answer explains how to format your numbers to fewer places.

Related Stack Overflow Question

I personally would use System.out.printf with a format string instead.

You can play with this format string. NOte that I added spaces for readability -- you would ultimately want to remove them once you're happy.

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

      for (int n = 16; n<2049; n*=2){
          //System.out.println(Math.log(n) +"\t"+ n + "\t"+ (Math.log(n)*n)+"\t"+Math.pow(n,2)+"\t"+Math.pow(n,3)+"\t"+Math.pow(2,n)+"\t");
          System.out.printf( "%12.5f\t %6d\t %12.5f\t %14.0f\t %14.0f\t %16.0f\n",
              Math.log(n), n,
              Math.log(n)*n,
              Math.pow(n,2),
              Math.pow(n,3),
              Math.pow(2,n));
      }
  }
}

This isn't perfect because the final column gets really big really fast. But I end up with output like this:

 2.77259         16      44.36142               256            4096             65536
 3.46574         32     110.90355              1024           32768        4294967296
 4.15888         64     266.16852              4096          262144  18446744073709552000
 4.85203        128     621.05987             16384         2097152  340282366920938460000000000000000000000
 5.54518        256    1419.56543             65536        16777216  115792089237316200000000000000000000000000000000000000000000000000000000000000
 6.23832        512    3194.02221            262144       134217728  13407807929942597000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 6.93147       1024    7097.82713           1048576      1073741824          Infinity
 7.62462       2048   15615.21968           4194304      8589934592          Infinity
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • Thank you! I was initially going to using System.out.printf but the question specifically asked for tabs so I wasn't sure. This works! – BipoN Oct 21 '19 at 22:44
0

Do something like this:

System.out.print(format(n) + "\t");
System.out.print(format(Math.log(n)) + "\t");
System.out.print(format(Math.log(n)*n) + "\t");

and define a function 'format' that returns a string and prepend n-input.length spaces so that every input is the same length.

Christian B
  • 102
  • 3