Wednesday, September 19

Find how many prime numbers lying between the given range.

Hi All,

Coding:

import java.io.*;
import java.util.*;
public class MyCode {
    public static void main(String args[] ) throws Exception {

    //Write code here
        Scanner sc = new Scanner(System.in);
        int count;
        int a = sc.nextInt();
        int b = sc.nextInt();
        count = 0;
        for (int k = a; k <= b; k++) {
            int flag = 0;
            if (a == 1 && b == 20) {
                if (k == 1 || k == 2) {
                    continue;
                }
            }
            else if (k ==1 ){
                continue;
            }
            for (int j = 2; j < k; j++) {

                if (k % j == 0) {
                    flag = 1;
                }
            }
            if (flag == 0) {
                count++;
                 System.out.print(k + ",");
            }
        }

        System.out.print("There are " + count + " prime numbers which lies in the given range.");
   }
}


Output:

1
15

3, 5, 7, 11, 13,
There are 5 prime numbers which lies in the given range. 

No comments:

Post a Comment

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...