Write a Java program to print a table of a number entered by the user

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Solution

import java.util.Scanner;
public class ClassName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
		System.out.print("Enter a number ");
		int num = sc.nextInt();
		System.out.println("\nTable of " + num);
		for (int i = 1; i <= 10; i++) {
			System.out.println(num + "*" + i + "=" + num * i);
		}
	}
}
Output
Enter a number 18

Table of 18
18*1=18
18*2=36
18*3=54
18*4=72
18*5=90
18*6=108
18*7=126
18*8=144
18*9=162
18*10=180

Explanation

  • Scanner class is used to take input from a user to print a table of a number.
  • The “for” loop is used to iterate the calculation and print the multiplication of a number.
  • The “for” loop initializes the value of “i” with 1, checks the condition and iterates the multiplication till 10, and increments the “i” by 1 with each iteration. When the value of “i” becomes 11, the condition becomes false and the loop terminates.
QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.