How to align the output in java from right?

10,543

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);

    }
}
Share:
10,543
Milan kc
Author by

Milan kc

Updated on June 15, 2022

Comments

  • Milan kc
    Milan kc almost 2 years

    I want to align my output in java as follows:

       1234
      *3406
    ----------------
       7404
         0
     4936
    3702
    -----------------
    4201004
    
    • hansvb
      hansvb over 10 years
      Align in which medium? Console output? HTML? Swing?