Java Programs


Genarate a Factorial Table


  • Recursive function to compute the factorial to a given number. Use the function to write a program which will generate a table of factorial of numbers ranging from 1 to m where m is the number entered by the user.

Program

import java.util.Scanner;

public class FactorialTable {

            private static Scanner sc;
            static int f=1;
            public static int factorial(int n) {
                       
                        if(n>0) {
                                    f=n*FactorialTable.factorial(n-1);
                        }
                        return f;
            }
            public static void main(String[] args) {
                       
                        int num,fact;
                        sc=new Scanner(System.in);
                        System.out.println("Enter the limit:");
                        num=sc.nextInt();
                        System.out.println("Number\t\tFactorial");
                        System.out.println("------\t\t---------");
                        for(int i=0;i<=num;i++) {
                                    fact=FactorialTable.factorial(i);
                                    System.out.println(+i+"\t\t"+fact);
                                    f=1;
                                    }
                       
            }

}

Output

Enter the limit:
10
Number            Factorial
------                 ---------
0                      1
1                      1
2                      2
3                      6
4                      24
5                      120
6                      720
7                      5040
8                      40320
9                      362880
10                    3628800


Multiplication Table


  • Program to print the multiplication table from 1 to m for n where m, n is the values entered by the user.

Program

import java.util.Scanner;

public class MultiplicationTable {

            private static Scanner sc;
            public static void main(String[] args) {
                       
                        int n,m;
                        sc=new Scanner(System.in);
                        System.out.println("Enter the limit:");
                        m=sc.nextInt();
                        System.out.println("Enter the number:");
                        n=sc.nextInt();
                        for(int j=1;j<=m;j++) {
                        System.out.println("--------------------------------------");
                        System.out.println("* Multiplication Table of "+j+" up to "+n+" *");
                        System.out.println("--------------------------------------");
                        for(int i=1;i<=n;i++) {
                                    System.out.println(+i+" x "+j+" = "+(j*i));
                        }
                        }

            }

}

Output

Enter the limit:
3
Enter the number:
4
--------------------------------------
* Multiplication Table of 1 up to 4 *
--------------------------------------
1 x 1 = 1
2 x 1 = 2
3 x 1 = 3
4 x 1 = 4
--------------------------------------
* Multiplication Table of 2 up to 4 *
--------------------------------------
1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
--------------------------------------
* Multiplication Table of 3 up to 4 *
--------------------------------------
1 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12


No comments:

Post a Comment