Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Multiplication table of any numbers

In this example, we're going to generate the multiplication table of any number. We'll demonstrate how to do this using different programming languages, including C, C++, Python, and Java. This exercise is helpful for understanding basic arithmetic operations and loop structures in programming.

(a.) C Code

#include <stdio.h>

    int main() {
        int num, i;

        printf("Enter a number: ");
        scanf("%d", &num);

        for(i = 1; i <= 10; i++) {
            printf("%d x %d = %d
", num, i, num * i);
        }

        return 0;
    }
Output:-
Enter a number: 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

(b.) C++ Code

#include <iostream>

    using namespace std;

    int main() {
        int num;

        cout << "Enter a number: ";
        cin >> num;

        for(int i = 1; i <= 10; i++) {
            cout << num << " x " << i << " = " << num * i << endl;
        }

        return 0;
    }
Output:-
Enter a number: 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

(c.) Python Code

def main():
        num = int(input("Enter a number: "))

        for i in range(1, 11):
            print(f"{num} x {i} = {num * i}")

    if __name__ == "__main__":
        main()
Output:-
Enter a number: 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

(d.) Java Code

import java.util.Scanner;

    public class MultiplicationTable {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int num = scanner.nextInt();

            for (int i = 1; i <= 10; i++) {
                System.out.println(num + " x " + i + " = " + num * i);
            }
        }
    }
Output:-
Enter a number: 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