Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Print Multiplication Table for Any Number in Java



For a given integer number, write a Java program to print its multiplication table. In mathematics, a multiplication table shows the product of two numbers.

Printing Multiplication Table using for Loop

In Java, a for loop is used when a specific block of code needs to be executed multiple times. Here, we will run this loop 10 times and increment the loop variable by one with each iteration. During this iteration, multiply the given integer value with the current value of the loop variable to get the multiplication table.

Example

Following is a Java program which prints the multiplication table of the given integer value.

public class MultiplicationTable {
   public static void main(String args[]) {
      int num = 17;
      System.out.println("Given integer value: " + num );
      for(int i=1; i<= 10; i++) {
         System.out.println(""+num+" X "+i+" = "+(num*i));
      }
   }
}

When you run this code, following output will be displayed ?

Given integer value: 17
17 X 1 = 17
17 X 2 = 34
17 X 3 = 51
17 X 4 = 68
17 X 5 = 85
17 X 6 = 102
17 X 7 = 119
17 X 8 = 136
17 X 9 = 153
17 X 10 = 170

Printing Multiplication Table using while Loop

In this approach, we will use while loop to implement the logic discussed above.

Example

This is another Java program which prints the multiplication table using while loop.

public class MultiplicationTable {
   public static void main(String args[]) {
      int num = 11;
      System.out.println("Given integer value: " + num);
      int i = 1;
      while (i <= 10) {
         System.out.println("" + num + " X " + i + " = " + (num * i));
         i++;
      }
   }
}

On running, the above code will show below result ?

Given integer value: 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
11 X 10 = 110
Updated on: 2024-08-01T10:58:04+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements