📜  递归程序打印数字的乘法表

📅  最后修改于: 2021-05-07 00:41:25             🧑  作者: Mango

给定数字N ,任务是使用递归打印其乘法表。
例子

递归方法打印数字乘法表

方法:

  1. 获取要为其打印乘法表的数字。
  2. 从值1递归地迭代到10:
    • 基本情况:如果递归调用的值大于10,则从函数退出。
if(i > N) 
  return ;
  • 递归调用:如果不满足基本条件,则为该值打印其乘法表,然后调用该函数进行下一次迭代。
print("N*i = ", N*i)
recursive_function(N, i+1);
  • 返回语句:在每个递归调用(基本情况除外)中,返回递归函数以进行下一次迭代。
return recursive_function(N, i+1);

下面是上述方法的实现:

C++
// C++ program to print table
// of a number using recursion
#include 
using namespace std;
 
// Function that print the
// table of a given number
// using recursion
void mul_table(int N, int i)
{
    // Base Case
    if (i > 10)
        return;
 
    // Print the table for
    // current iteration
    cout << N << " * " << i
         << " = " << N * i
         << endl;
 
    // Recursive call to next
    // iteration
    return mul_table(N, i + 1);
}
 
// Driver Code
int main()
{
    // Input number whose table
    // is to print
    int N = 8;
 
    // Function call to print
    // the table
    mul_table(N, 1);
    return 0;
}


Java
// Java program to print table
// of a number using recursion
class GFG {
     
    // Function that print the
    // table of a given number
    // using recursion
    static void mul_table(int N, int i)
    {
        // Base Case
        if (i > 10)
            return ;
     
        // Print the table for
        // current iteration
        System.out.println(N + " * " + i + " = " + N * i);
                
        // Recursive call to next
        // iteration
        mul_table(N, i + 1);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        // Input number whose table
        // is to print
        int N = 8;
     
        // Function call to print
        // the table
        mul_table(N, 1);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to print table
# of a number using recursion
 
# Function that print the
# table of a given number
# using recursion
def mul_table(N, i):
     
    # Base Case
    if (i > 10):
        return
     
    # Print the table for
    # current iteration
    print(N,"*",i,"=",N * i)
     
    # Recursive call to next
    # iteration
    return mul_table(N, i + 1)
 
# Driver Code
 
# Input number whose table
# is to print
N = 8
 
# Function call to print
# the table
mul_table(N, 1)
 
# This is contributed by shubhamsingh10


C#
// C# program to print table
// of a number using recursion
using System;
 
class GFG{
     
    // Function that print the
    // table of a given number
    // using recursion
    static void mul_table(int N, int i)
    {
        // Base Case
        if (i > 10)
            return ;
     
        // Print the table for
        // current iteration
        Console.WriteLine(N + " * " + i + " = " + N * i);
                
        // Recursive call to next
        // iteration
        mul_table(N, i + 1);
    }
     
    // Driver Code
    public static void Main()
    {
        // Input number whose table
        // is to print
        int N = 8;
     
        // Function call to print
        // the table
        mul_table(N, 1);
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

时间复杂度: O(1)