Java Program to Print Multiplication Table for Any Number
Last Updated :
30 Jan, 2023
Given a number n as input, we need to print its table, where N>0.
Example
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
Two ways are shown to Print Multiplication Table for any Number:
- Using for loop for printing the multiplication table upto 10.
- Using while loop for printing the multiplication table upto the given range.
Method 1: Generating Multiplication Table using for loop upto 10
Java
class GFG {
public static void main(String[] args)
{
int N = 7 ;
for ( int i = 1 ; i <= 10 ; i++) {
System.out.println(N + " * " + i + " = "
+ N * i);
}
}
}
|
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
Method 2:- Generating Multiplication Table using while loop upto any given range
Java
class GFG {
public static void main(String[] args)
{
int N = 7 ;
int range = 18 ;
int i = 1 ;
while (i <= range) {
System.out.println(N + " * " + i + " = "
+ N * i);
i++;
}
}
}
|
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
7 * 11 = 77
7 * 12 = 84
7 * 13 = 91
7 * 14 = 98
7 * 15 = 105
7 * 16 = 112
7 * 17 = 119
7 * 18 = 126
Time complexity: O(n) where n is given input range
Auxiliary space: O(1)
Method 3: Generating multiplication table of any number by using function.
Java
import java.io.*;
import java.util.Scanner;
class Program
{
static void table( int no)
{
for ( int i = 1 ; i<= 10 ; i++)
{
System.out.print(i*no+ " " );
}
}
public static void main(String[] args)
{
System.out.println( "Table of 6=" );
table( 6 );
System.out.println( "\nTable of 5=" );
table( 5 );
}
}
|
Output
Table of 6=
6 12 18 24 30 36 42 48 54 60
Table of 5=
5 10 15 20 25 30 35 40 45 50
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...