Page 163 - CA_Blue( J )_Class10
P. 163

The above program continues as long as the value of i is less than or equal to j.

                  Program 2    To input a number and print whether the number is prime number or not.

                   1  import java.util.*;

                   2  class prime_number
                   3  {

                   4      public static void main ()
                   5      {

                   6          Scanner sc= new Scanner (System.in);
                   7          int n,i,count=0;

                   8          System.out.print("Enter a number: ");
                   9          n=sc.nextInt();

                  10          for (i=1; i<=n;i++)
                  11          {

                  12              if(n%i==0)
                  13              {

                  14                  count++;
                  15              }

                  16          }
                  17          if(count==2)

                  18              System.out.println(n+" is Prime Number");
                  19          else

                  20              System.out.println(n+" is not Prime Number");
                  21      }
                  22  }


                 You will get the following output:

























                                                                                                                       161
                                                                                              Iterative constructs in Java  161
   158   159   160   161   162   163   164   165   166   167   168