If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To generate the table (multiplication) of a given number in Java, you can use a loop to calculate the product of the number with integers from 1 to a specified range (usually 10). Here's a Java program to display the table of a given number:
javaCopy code
import java.util.Scanner;
public class NumberTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to generate its table: ");
int number = scanner.nextInt();
// Display the table of the given number
System.out.println("Table of " + number + ":");
for (int i = 1; i <= 10; i++) {
int product = number * i;
System.out.println(number + " x " + i + " = " + product);
}
scanner.close();
}
}
Example output:
cssCopy code
Enter a number to generate its table: 5
Table of 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
In the program above, we take the user input for the number and store it in the variable number
. We then use a for
loop to iterate from 1 to 10. Inside the loop, we calculate the product of the number with the current iterator value (i
) and print the table entries in the specified format.
You can adjust the loop range to generate tables of different sizes or modify the program to take the range as user input if you want more flexibility.
Comments: 0