Write a program to print tables


This Java program prompts the user to input three integers: a starting number, an ending number, and a tables number. It then uses a while loop to print the multiplication table of the specified number from the starting number to the ending number, inclusive.

Here's a step-by-step breakdown of how the program works:

  • The program starts by importing the java.util.Scanner class, which is used to read user input from the console.
  • The Tables class is defined and the main method is declared.
  • Inside the main method, an instance of the Scanner class is created and assigned to a variable named input.
  • The program prompts the user to input the starting number by printing the message "Enter The Starting Number : " to the console. The nextInt() method of the Scanner class is then used to read an integer value from the console and assign it to a variable named s.
  • The program prompts the user to input the ending number by printing the message "Enter The Ending Number : " to the console. The nextInt() method of the Scanner class is then used to read an integer value from the console and assign it to a variable named e.
  • The program prompts the user to input the tables number by printing the message "Enter The Tables Number : " to the console. The nextInt() method of the Scanner class is then used to read an integer value from the console and assign it to a variable named t.
  • The program enters a while loop that will execute as long as the value of s is less than or equal to the value of e.
  • Inside the while loop, the program prints the current value of s, the tables number t, and the product of s and t to the console using the println() method of the System.out object.
  • The value of s is then incremented by 1 using the ++ operator.
  • The program returns to the beginning of the while loop and checks the condition again. If the value of s is still less than or equal to the value of e, the loop will continue to execute. If not, the loop will terminate and the program will exit.

Source Code

import java.util.Scanner;
class Tables
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Starting Number : ");
		int s =input.nextInt();
		System.out.print("Enter The Ending Number : ");
		int e =input.nextInt();
		System.out.print("Enter The Tables Number : ");
		int t =input.nextInt(); 
		while(s<=e)
		{
			System.out.println(s+" * "+t+" = "+(s*t));
			s++;
		}
	}
}

Output

Enter The Starting Number : 1
Enter The Ending Number : 10
Enter The Tables Number : 3
1 * 3 = 3
2 * 3 = 6
3 * 3 = 9
4 * 3 = 12
5 * 3 = 15
6 * 3 = 18
7 * 3 = 21
8 * 3 = 24
9 * 3 = 27
10 * 3 = 30

Example Programs