Page 348 - CA_Blue( J )_Class10
P. 348

Program 3    To print the factorial of a number.


                 1  import java.util.*;

                 2  class derived_class extends protected_access_sp
                 3  {
                 4      int f;

                 5      void factorial()
                 6      {

                 7         int i;
                 8         accept();

                 9         f=1;
                10         for(i=1;i<=n;i++)

                11         {
                12            f=f*i;
                13         }

                14      }
                15      void display()

                16      {
                17         System.out.println("The factorial of "+n + " is : "+ f);

                18      }
                19

                20      public static void main()
                21      {
                22      derived_class ob=new derived_class();

                23      ob.factorial();
                24      ob.display();

                25      }
                26      }

                27  class protected_access_sp
                28  {

                29      protected int n;
                30      void accept()
                31      {

                32         Scanner sc= new Scanner(System.in);




                346346  Touchpad Computer Applications-X
   343   344   345   346   347   348   349   350   351   352   353