Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmMultiplication table till N rows where every Kth row is table of...

Multiplication table till N rows where every Kth row is table of K upto Kth term

Given a number N, the task is to print N rows where every Kth row consists of the multiplication table of K up to Kth term. 

Examples: 

Input: N = 3 
Output: 

2 4 
3 6 9 
Explanation: 
In the above series, in every Kth row, multiplication table of K upto K terms is printed.

Input: N = 5 
Output: 

2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 

Approach: The idea is to use two for loops to print the multiplication table. The outer loop in ‘i’ serves as the value of ‘K’ and the inner loop in ‘j’ serves as the terms of the multiplication table of every ‘i’. Each term in the table of ‘i’ can be obtained with the formula ‘i * j’. 

Below is the implementation of the above approach: 

C++




// C++ program to print multiplication table
// till N rows where every Kth row
// is the table of K up to Kth term
#include <iostream>
using namespace std;
 
// Function to print the multiplication table
// upto K-th term
void printMultiples(int N)
{
    // For loop to iterate from 1 to N
    // where i serves as the value of K
    for (int i = 1; i <= N; i++)
    {
 
        // Inner loop which at every
        // iteration goes till i
        for (int j = 1; j <= i; j++)
        {
 
            // Printing the table value for i
            cout << (i * j) << " ";
        }
 
        // New line after every row
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int N = 5;
 
    printMultiples(N);
 
    return 0;
}
 
// This code is contributed by Rajput-Ji


Java




// Java program to print multiplication table
// till N rows where every Kth row
// is the table of K up to Kth term
 
class GFG {
 
    // Function to print the multiplication table
    // upto K-th term
    public static void printMultiples(int N)
    {
        // For loop to iterate from 1 to N
        // where i serves as the value of K
        for (int i = 1; i <= N; i++) {
 
            // Inner loop which at every
            // iteration goes till i
            for (int j = 1; j <= i; j++) {
 
                // Printing the table value for i
                System.out.print((i * j) + " ");
            }
 
            // New line after every row
            System.out.println();
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        int N = 5;
 
        printMultiples(N);
    }
}


Python3




# Python3 program to pr multiplication table
# till N rows where every Kth row
# is the table of K up to Kth term
 
# Function to pr the multiplication table
# upto K-th term
def prMultiples(N):
 
    # For loop to iterate from 1 to N
    # where i serves as the value of K
    for i in range(1, N + 1):
 
        # Inner loop which at every
        # iteration goes till i
        for j in range(1, i + 1):
 
            # Printing the table value for i
            print((i * j), end = " ")
 
        # New line after every row
        print()
 
# Driver code
if __name__ == '__main__':
    N = 5
 
    prMultiples(N)
 
# This code is contributed by mohit kumar 29


C#




// C# program to print multiplication table
// till N rows where every Kth row
// is the table of K up to Kth term
using System;
 
class GFG
{
 
    // Function to print the multiplication table
    // upto K-th term
    public static void printMultiples(int N)
    {
        // For loop to iterate from 1 to N
        // where i serves as the value of K
        for (int i = 1; i <= N; i++) {
 
            // Inner loop which at every
            // iteration goes till i
            for (int j = 1; j <= i; j++) {
 
                // Printing the table value for i
                Console.Write((i * j) + " ");
            }
 
            // New line after every row
            Console.WriteLine();
        }
    }
 
    // Driver code
    public static void Main(String []args)
    {
        int N = 5;
 
        printMultiples(N);
    }
}
 
// This code is contributed by Rajput-Ji


Javascript




<script>
 
// javascript program to print multiplication table
// till N rows where every Kth row
// is the table of K up to Kth term
 
 
// Function to print the multiplication table
// upto K-th term
function printMultiples( N)
{
    // For loop to iterate from 1 to N
    // where i serves as the value of K
    for (let i = 1; i <= N; i++)
    {
 
        // Inner loop which at every
        // iteration goes till i
        for (let j = 1; j <= i; j++)
        {
 
            // Printing the table value for i
           document.write((i * j) + " ");
        }
 
        // New line after every row
       document.write("<br/>");
    }
}
 
// Driver code
 
    let N = 5;
 
    printMultiples(N);
     
// This code is contributed by todaysgaurav
 
 
</script>


Output: 

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25

 

Time Complexity: O(N2), here there are two loops with one running from 1 to N and another from 1 to i which means it is also running N times so it is a N*N complexity
Auxiliary Space: O(1), no extra array is being used so the space required is constant

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments