Multiplication table Learn programming Java

Lesson:

First contact with Java


Exercise:

Multiplication table


Objetive:

Write a java program to ask the user for a number and display its multiplication table, like this:

Please enter a number:
5

The multiplication table for 5 is:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int x;
		int result;
		System.out.println("Enter a number:");
		x = Integer.parseInt(new Scanner(System.in).nextLine());
		result = x * 0;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 0, result);
		result = x * 1;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 1, result);
		result = x * 2;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 2, result);
		result = x * 3;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 3, result);
		result = x * 4;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 4, result);
		result = x * 5;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 5, result);
		result = x * 6;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 6, result);
		result = x * 7;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 7, result);
		result = x * 8;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 8, result);
		result = x * 9;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 9, result);
		result = x * 10;
		System.out.printf("The result is: %1$s x %2$s = %3$s" + "\r\n", x, 10, result);
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.