Page 222 - ComputerScience_Class_11
P. 222

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(String args[])

                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:

                     BlueJ: Terminal Window - Java
                 Options

                Enter the starting value:
                10
                Enter the ending value:
                50
                11 13 17 19 23 29 31 37 41 43 47



                  220  Touchpad Computer Science (Ver. 3.0)-XI
   217   218   219   220   221   222   223   224   225   226   227