2

I want to align my output in java as follows:

   1234
  *3406
----------------
   7404
     0
 4936
3702
-----------------
4201004
Thilo
  • 257,207
  • 101
  • 511
  • 656
Milan kc
  • 21
  • 1
  • 1
  • 2

2 Answers2

1

Use System.out.printf like below:

public class Multi {
    public static void main(String[] args) {
        int align=10;
        int a=1234;
        int b=3406;
         System.out.printf("%"+align+"d\n",a);
         System.out.printf("%"+align+"d\n",b);
         System.out.println("---------------------------");
        int res=a*b;
        while(b!=0){
            int t=b%10;            
            System.out.printf("%"+(align--)+"d\n",t*a);
            b/=10;
        }
        System.out.println("---------------------------");
        System.out.printf("%10d\n",res);

    }
}
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

you can use String.format

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...)

http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

specifically, patterns like

"%8d"

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66