Program to Print Multiplication Table of a Number
Last Updated :
15 Feb, 2024
Given two positive integers N and K, the task is to print the Multiplication table of N till Kth multiple.
Examples:
Input: N = 6, K = 5
Output:
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
Input: N = 9, K = 3
Output:
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
Approach:
The problem can be solved by running a loop from i = 1 to i = K and for every iteration print the product of N and i as the answer.
Step-by-step approach:
- Read input values for N and K.
- Iterate for each integer from 1 to K using a For Loop.
- During each ith iteration print the ith multiple of N as N * i.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int main()
{
int N = 6;
int K = 10;
cout << "The multiplication table of " << N << endl;
for ( int i = 1; i <= K; i++) {
cout << N << " * " << i << " = " << N * i << endl;
}
}
|
Java
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
int N = 6 ;
int K = 10 ;
System.out.println( "The multiplication table of " + N);
for ( int i = 1 ; i <= K; i++) {
System.out.println(N + " * " + i + " = " + (N * i));
}
}
}
|
Python3
N = 6
K = 10
print (f "The multiplication table of {N}" )
for i in range ( 1 , K + 1 ):
print (f "{N} * {i} = {N * i}" )
|
C#
using System;
public class GFG {
static public void Main()
{
int N = 6;
int K = 10;
Console.WriteLine( "The multiplication table of "
+ N);
for ( int i = 1; i <= K; i++) {
Console.WriteLine(N + " * " + i + " = " + N * i);
}
}
}
|
Javascript
let N = 6;
let K = 10;
console.log( "The multiplication table of " + N);
for (let i = 1; i <= K; i++) {
console.log(N + " * " + i + " = " + N * i);
}
|
Output
The multiplication table of 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
Time Complexity: O(K), where K is the number of rows in the multiplication table.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...