Page 388 - computer science (868) class 11
P. 388

5           Prime(int nn)
                 6           {

                 7               num=nn;
                 8           }

                 9           int countfactors(int i)
                10           {

                11               if(i>num) // base case
                12                   return 0;
                13               else if(num%i==0) // factors

                14                   return 1+countfactors(i+1); // count increases by 1

                15               else
                16                   return countfactors(i+1);
                17           }

                18           void check()
                19           {

                20               if(countfactors(1)==2)// if count of factor=2
                21                   System.out.println(num + " is a Prime Number"); //display

                22               else
                23                   System.out.println(num + " is not a Prime Number");

                24           }
                25           public static void main(int x)

                26           {
                27               Prime obj = new Prime(x);

                28               obj.check();
                29           }

                30       }

              The output of the preceding program is as follows:

















              3 is a Prime Number


                386386  Touchpad Computer Science-XI
   383   384   385   386   387   388   389   390   391   392   393