public class MultiplicationTable
{
public static void main(String[] args)
{
int no = 7;
System.out.println("Multiplication Table of " + no + ":");
// Loop from 1 to 10 to generate the table
for (int i = 1; i <= 10; i++)
{
int result = no * i;
System.out.println(no + " x " + i + " = " + result);
}
}
}
Multiplication Table of 7: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
import java.util.Scanner;
public class MultiplicationTable
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a number
System.out.print("Enter a number to print its multiplication table: ");
int no = scanner.nextInt();
System.out.println("\nMultiplication Table of " + no + ":");
// Loop from 1 to 10 to generate the table
for (int i = 1; i <= 10; i++)
{
int result = no * i;
System.out.println(no + " x " + i + " = " + result);
}
// Close the scanner
scanner.close();
}
}
Enter a number to print its multiplication table: 10 Multiplication Table of 10: 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 10 x 10 = 100
Scanner
class is imported to take user input.
no
) is declared to store the user’s input.
scanner.nextInt()
is used to read and store the input in the no
variable.
for
Loop:
i = 1
and iterates up to i = 10
.
no
and i
.
no x i = product
.
scanner.close()
method is used to close the input stream and release resources.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.