📜  打印任意数乘法表的Java程序

📅  最后修改于: 2022-05-13 01:55:40.374000             🧑  作者: Mango

打印任意数乘法表的Java程序

给定一个数字 n 作为输入,我们需要打印它的表格,其中 N>0。

例子

Input 1 :- N = 7
Output  :- 
     7 * 1 = 7
    7 * 2 = 14
    7 * 3 = 21
    7 * 4 = 28
    7 * 5 = 35
    7 * 6 = 42
    7 * 7 = 49
    7 * 8 = 56
    7 * 9 = 63
    7 * 10 = 70

显示了两种打印任何数字的乘法表的方法:

  1. 使用 for 循环打印最多 10 的乘法表。
  2. 使用 while 循环将乘法表打印到给定范围。

方法 1:使用 for 循环最多 10 生成乘法表

Java
// Java Program to print the multiplication table of the
// number N.
  
class GFG {
    public static void main(String[] args)
    {
        // number n for which we have to print the
        // multiplication table.
        int N = 7;
  
        // looping from 1 to 10 to print the multiplication
        // table of the number.
        // using for loop
        for (int i = 1; i <= 10; i++) {
            // printing the N*i,ie ith multiple of N.
            System.out.println(N + " * " + i + " = "
                               + N * i);
        }
    }
}


Java
// Java Program to print the multiplication table of
// number N using while loop
  
class GFG {
    public static void main(String[] args)
    {
        // number n for which we have to print the
        // multiplication table.
        int N = 7;
  
        int range = 18;
  
        // looping from 1 to range to print the
        // multiplication table of the number.
        int i = 1;
  
        // using while loop
        while (i <= range) {
  
            // printing the N*i,ie ith multiple of N.
            System.out.println(N + " * " + i + " = "
                               + N * i);
            i++;
        }
    }
}


输出
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

方法 2:- 使用 while 循环生成乘法表到任何给定范围

Java

// Java Program to print the multiplication table of
// number N using while loop
  
class GFG {
    public static void main(String[] args)
    {
        // number n for which we have to print the
        // multiplication table.
        int N = 7;
  
        int range = 18;
  
        // looping from 1 to range to print the
        // multiplication table of the number.
        int i = 1;
  
        // using while loop
        while (i <= range) {
  
            // printing the N*i,ie ith multiple of N.
            System.out.println(N + " * " + i + " = "
                               + N * i);
            i++;
        }
    }
}
输出
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
7 * 11 = 77
7 * 12 = 84
7 * 13 = 91
7 * 14 = 98
7 * 15 = 105
7 * 16 = 112
7 * 17 = 119
7 * 18 = 126