A java program to print multiplication table in java - Source code by DR YENY

To write Java program to print multiplication table use the code below:


 package multiplication.table;

import java.util.Scanner;

public class MultiplicationTable {

    public static void main(String[] args) {

        int number,i;

        Scanner sc = new Scanner(System.in);

        

        System.out.println ("Enter the Number");

        number=sc.nextInt();

        for( i = 1; i<=12;i++){

            System.out.println(number+"*"+i+"="+(number*i));

        }

        

    }

    

}

The detailed explanation of the code

1.The first line of the code specifies the package name "multiplication.table".

2. The second line imports the "Scanner" class from the "java.util" package, which is used to read user input from the console.

3. The "MultiplicationTable" class is defined on the third line.

4. The "main" method is declared on the fourth line. This is the entry point of the program that will be executed first.

5. Two variables are declared on the sixth and seventh lines: "number" and "i". "number" will hold the input number entered by the user and "i" is a counter variable used in the "for" loop.

6. A new Scanner object "sc" is created on the ninth line to read user input from the console.

7. On the eleventh line, the program prompts the user to enter a number by printing "Enter the Number" to the console.

8. The user input is read using the "nextInt" method of the Scanner object "sc" and is assigned to the "number" variable on the twelfth line.

9. The "for" loop on the thirteenth line iterates from 1 to 12, incrementing the "i" variable by 1 on each iteration.

10. Within the "for" loop, the program prints the multiplication table to the console using the "println" method. The statement "number+""+i+"="+(numberi)" calculates the product of the input number and the counter variable "i" and prints the result in the format of a multiplication table.

11. The closing curly brace on the seventeenth line ends the "for" loop.

12. The closing curly brace on the nineteenth line ends the "main" method.

In summary, this program prompts the user to enter a number and then generates a multiplication table for that number from 1 to 12 using a "for" loop.

The program uses the Scanner class to read user input and the println method to print the multiplication table to the console.






Comment on this in the comment section.



2 تعليقات

Kindly leave a comment

إرسال تعليق

Kindly leave a comment

أحدث أقدم