0

Hello I am trying to make a random Multiplication question generator with the answer in this style.

   850702
×     841
-------------------
   850702
 3402808× 
6805616××
-------------------
715440382
-------------------

But I am getting this.

 850702
×   841
-------------------
850702         (comment )
3402808×    <- in this lines
6805616××   <-|
-------------------
715440382
-------------------

When its multiply each number of the second number it doesnt gives the space for × sign that i want (check that arrow in the comment of given output) .

Here is the source code


import java.lang.Math.*;
class Mainto { 
    public static void main (String[] args) {
 
        //Adding sone gap between output and console
        System.out.println(" ");
 

  
        //Generating first number to multiply
  
        double a = Math.random()*(999999-100000+1)+100000;  
        long num1 = (long)a; 

 
        //Generating second number to multiply it
        double b = Math.random()*(999-10+1)+10;   
        long num2 = (long)b; 
 
 
        //printing the num1 and num2 of question
  
        System.out.println(" "+num1);  
        System.out.println("×   "+num2);  
        System.out.println("-------------------");  
 
 
        //Multiplying every single number of num2 with num1
        long mul=num2;
        String nothing="";
        while(mul!=0){
            long d= mul %10;
            System.out.println(d*num1+nothing);
            mul /=10;
            nothing+="×";
        }
 
        //Multiplying  number and printing it
        long c=num1*num2;
        System.out.println("-------------------");
        System.out.println(c);
        System.out.println("-------------------");
 
        //Adding some gap in every question to make it easy to read
 
        System.out.println(" ");
        System.out.println(" ");

    } 
}
akarnokd
  • 69,132
  • 14
  • 157
  • 192
Adarshyodha
  • 65
  • 1
  • 7

1 Answers1

2

You can use various utility libraries or pure java methods for this. If you just want to print a number padded to right, let's keep it simple and most Java way. So, System.out.printf method would be the most practical way.

System.out.printf("%20s%nx%19s%n", num1, num2);  
System.out.println("--------------------");

System.out.printf("%20s%n", d*num1+nothing);

System.out.println("--------------------");
System.out.printf("%20s%n", c);
System.out.println("--------------------"); 

Basically, you can think like this: Get nth %{number} pattern in first argument and fill (n+1)th argument justified to the right ends at {number}th position.

%n means newline.

mdoflaz
  • 551
  • 6
  • 19