Java multiplication table (2 different ways)
Here you can learn how to print the multiplication table in java programming language with 2 different approaches.
Approach :-1 "multiplication table in java using for loop"
package CodeYourslf.allPrograms.multiplicationJava;
import java.util.Scanner;
public class byUsingfor {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = sc.nextInt();
for(int i = 1; i <= 10; i++){
int mult = num * i;
System.out.println(num + " * "+i + " = " + mult);
}
}
}
Output
Approach :-2 "multiplication table in java using while loop"
package CodeYourslf.allPrograms.multiplicationJava;
import java.util.Scanner;
public class byUsingwhile {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num = sc.nextInt();
int i = 1;
while(i <= 10){
System.out.println(num + " * "+i + " = " + (num * i));
i++;
}
}
}
Code Explanation
Now I will explain how these above approaches are working.
Approach :- 1 explanation ..
Here i is started from 1 and it will go upto 10, and at each iteration i will be multiplied with that number(num * i) will give the multiplication table. Here i will be incremented with each iteration.
Approach :- 2 explanation..
Here the concept is totally same as for loop, only i will be declared previously of while loop.
๐ Hey Please share with your friends and if you learn something new from this article, then comment down below
If you have any questions or any query, don't feel bad to write it on the comment below ๐.