multiplication table of 10 java code example

Example 1: Java program to print multiplication table of any number

import java.util.Scanner;
public class MultiplicationTablesInJava
{
   public static void main(String[] args)
   {
      System.out.println("Please enter an integer to print tables : ");
      Scanner sc = new Scanner(System.in);
      int number = sc.nextInt();
      System.out.println("Multiplication table of " + number + " is : ");
      for(int a = 1; a <= 10; a++)
      {
         System.out.println(number + " * " + a + " = " + number * a);
      }
      sc.close();
   }
}

Example 2: Multiplication table for number Java

public static String multiTable(int num) {
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i <= 10; i++) {
            int result = i * num;
            sb.append(i + " * " + num + " = " + result + "\n");
        }
        return sb.toString().trim();
    }

Tags:

Java Example