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:
- The program begins with the #include directive, which allows the use of input/output functions in the program.
- 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.
- 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.
- The program prompts the user to enter a number by displaying the message “Enter a number: ” using the printf() function.
- The user’s input is read and stored in the num variable using the scanf() function.
- 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.
- 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.
- 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:
- The program starts by importing the Scanner class from the java.util package, which allows us to read input from the user.
- The program defines a public class named TableInJava.
- Inside the TableInJava class, the program declares the main method, which is the entry point of the program.
- The main method declares two integer variables, num and i, which will be used to store the user input and the loop counter, respectively.
- 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).
- The program prompts the user to enter a number by using System.out.print() to display the message “Enter a number: “.
- 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.
- The program enters a for loop that iterates from 1 to 10. The loop counter is stored in the i variable.
- 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”.
- After the loop finishes, the program calls the close() method on the Scanner object to release any system resources associated with it.
- 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