Page 456 - CA_Blue( J )_Class10
P. 456

8              {

                 9                  f=f*i;
                10              }

                11              System.out.println("Factorial of "+n +" : "+f);   // Printing of factorial
                12          }

                13          void main()
                14          {

                15              Scanner sc = new Scanner(System.in);
                16              int num;

                17              System.out.print("Enter a number to find factorial : ");
                18              num=sc.nextInt();           // Number to find factorial

                19              factorial(num);           // Invoking of function
                20          }

                21      }

              Output
              Enter a number to find factorial :
              5
              Factorial of 5 : 120
                                                         Variable Description
                 NAME       DATATYPE                                     DESCRIPTION
               i                int      Loop variable

               f                int      Stores the factorial of the number
               num              int      Actual Parameter for number to find factorial
               n                int      Formal Parameter for number to find factorial

              (c)  Program depicting the concept of static method.


                Program 3    Write a static method to take two numbers and print the Greatest Common Divisor of them.



                 1      import java.util.*;

                 2      class gcd_staticmethod
                 3      {

                 4          public static void gcdmethod(int a, int b)   // Prototype of gcdmethod()
                 5          {

                 6              int s, i, gcd=0;       // Variable Declaration

                 7              s=(a<b)?a:b;            // Ternary operator to find the small number



                454454  Touchpad Computer Applications-X
   451   452   453   454   455   456   457   458   459   460   461