Page 198 - Cs_withBlue_J_C11_Flipbook
P. 198

The output of the preceding program is as follows:
              Please enter a number: 145

              145 is a Krishnamurthy Number.


                Program 8     Write a Java program to input two numbers, e.g., sv (starting value) and ev (ending value)
                              and print all the prime numbers between sv and ev.

                1       import java.util.*;

                2       class prime
                3       {
                4           public static void main()

                5           {

                6               //Declaring variable and taking value from user...
                7               Scanner sc = new Scanner(System.in);
                8               int sv, ev, i, j, c;

                9               System.out.println("Enter the starting value : ");
                10              sv=sc.nextInt();

                11              System.out.println("Enter the ending value : ");
                12              ev=sc.nextInt();

                13              for(i=sv; i<=ev; i++)
                14              {

                15                  c=0;
                16                  for(j=1; j<=i; j++)

                17                  {
                18                      if(i%j==0)

                19                      {
                20                          c++;

                21                      }
                22                  }

                23                  if(c==2)
                24                      System.out.print(i+ " ");

                25              }
                26          }

                27      }

              The output of the preceding program is as follows:
              Enter the starting value:
              10


                196196  Touchpad Computer Science-XI
   193   194   195   196   197   198   199   200   201   202   203