Page 214 - CA_Blue( J )_Class9
P. 214

You will get the following output:







































                   Program 3      Write a program to find the factorial of a number.

                     1     import java.util.Scanner;

                     2     class Factorial
                     3     {

                     4     public static void main(String args[])
                     5     {

                     6     int factorial = 1;
                     7     Scanner f = new Scanner(System.in);
                     8     System.out.print("Enter a number: ");

                     9     int num= f.nextInt();

                    10     for (int i = 1; i <= num; i++)
                    11     {
                    12         factorial *= i;                            //or use factorial = factorial*i

                    13     }
                    14     System.out.println(num + "!= " + factorial);

                    15     f.close();
                    16     }}





                   212    Touchpad Computer Applications-IX
   209   210   211   212   213   214   215   216   217   218   219