Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Program to get multiplication table

Write a program that prints the multiplication table of a given number.

Program in C

C
#include <stdio.h>

int main() {
    int num, i;
    
    // Get input from user
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Print table up to 10
    for (i = 1; i <= 10; i++) {
        printf("%d x %d = %d\n", num, i, num * i);
    }
    
    return 0;
}

Explanation:

  1. The program begins with the #include directive, which allows the use of input/output functions in the program.
  2. The main() function is the entry point of the program. It has a return type of int, indicating that it should return an integer value when it completes.
  3. Inside the main() function, two variables are declared: num and i. num will store the input number from the user, and i will be used as a counter for the for loop.
  4. The program prompts the user to enter a number by displaying the message “Enter a number: ” using the printf() function.
  5. The user’s input is read and stored in the num variable using the scanf() function.
  6. A for loop is used to iterate from 1 to 10. The loop variable i is initialized to 1, and the loop continues as long as i is less than or equal to 10. After each iteration, i is incremented by 1.
  7. Inside the for loop, the printf() function is used to display the multiplication table for the given number. It prints the format string “%d x %d = %d\n”, where the first %d represents the num variable, the second %d represents the loop variable i, and the third %d represents the result of multiplying num and i.
  8. Finally, the main() function ends with a return 0; statement, indicating that the program executed successfully.

Output:

Output
Enter a number: 4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40

Program in Java

Java
import java.util.Scanner;

public class TableInJava {
    public static void main(String[] args) {
        int num, i;

        // Get input from user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        num = scanner.nextInt();

        // Print table up to 10
        for (i = 1; i <= 10; i++) {
            System.out.printf("%d x %d = %d\n", num, i, num * i);
        }

        scanner.close();
    }
}

Explanation:

  1. The program starts by importing the Scanner class from the java.util package, which allows us to read input from the user.
  2. The program defines a public class named TableInJava.
  3. Inside the TableInJava class, the program declares the main method, which is the entry point of the program.
  4. The main method declares two integer variables, num and i, which will be used to store the user input and the loop counter, respectively.
  5. The program creates a Scanner object named scanner to read input from the user. It uses System.in as the input source, which represents the standard input (keyboard).
  6. The program prompts the user to enter a number by using System.out.print() to display the message “Enter a number: “.
  7. The program uses the nextInt() method of the Scanner class to read an integer input from the user and stores it in the num variable.
  8. The program enters a for loop that iterates from 1 to 10. The loop counter is stored in the i variable.
  9. Inside the loop, the program uses System.out.printf() to print the multiplication table. It displays the value of num, i, and the product of num and i in the format “num x i = num * i”.
  10. After the loop finishes, the program calls the close() method on the Scanner object to release any system resources associated with it.
  11. The program execution completes, and the program terminates.
Output
Enter a number: 4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40