Printing Table of a Particular Number in Java
Today we will see how to print the table of a particular number in Java.
This program basically deals with output formatting, that is, getting output in the format “5 x 1 =5” and so on.
If the number is N, then the formatting should be as:
N x i = (N*i)
where “i” is the loop variable which ranges from 1 to 10.
If the user enters: 5
The output should be in the following format:
5 x 1 =5 5 x 2=10 . . 5 x 10=50
You may also check: Make A Multiplication Table in Java with a specific range
Okay, let’s go straight to the code.
import java.util.*; public class test { public static void main(String args[]) { int num; Scanner sc =new Scanner(System.in); System.out.println("Enter number whose table you want "); num=sc.nextInt(); for(int i=1;i<=10;i++) { System.out.println(num+" x "+i+" = "+ num*i); } } }
The above code will print the table in the correct format as follows:
Enter number whose table you want 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
Enter number whose table you want 23 23 x 1 = 23 23 x 2 = 46 23 x 3 = 69 23 x 4 = 92 23 x 5 = 115 23 x 6 = 138 23 x 7 = 161 23 x 8 = 184 23 x 9 = 207 23 x 10 = 230
Similarly, this program can work for any given number.
Hope this helped you out.
Have a nice day ahead.
Leave a Reply