Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Saturday, August 10, 2013

Java program to generate Prime numbers between given two numbers ! ! !

Prime number Generator :

It can also be considered as a solution to the SPOJ classic problem it has some code exceptions which can also be by pass using try and catch method of java exception handling*********









 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * @author CodeAuthor
 * 
 */

import java.util.*;
class PRIME1 {
 
static void prime(int m,int n)
{ 
    if(m>n)
        return ;
    int j=2;
    boolean flag=true;
    while(j<m)
        {
            if(m%j==0)
                flag = false;
            j++;
        }
    if(flag)
      System.out.print(m + " "); //printing the numbers
     prime(m+1,n);  //recalling self 
}
   
public static void main(String[] args) {
        Scanner c = new Scanner(System.in);
        int m,g,n;
        System.out.println("Enter number of test cases : ");
        int t=c.nextInt(); //getting the test cases
       
           while (t > 0)
        {
           System.out.println("Enter the starting and ending point between which prime numbers to be shown : ");
             m=c.nextInt(); //get starting number
             n = c.nextInt(); //get ending number
             
             System.out.print("The prime numbers between "+ m +" and " + n + " are :" );
             //check for if m==1 or m==0
             if(m==0 || m==1)
                 m=2;
             prime(m,n); //call the prime function

        System.out.println();
        t--;
            
        }
       }
}
The output for the program is --- - - - - -







Enter number of test cases : 
3
Enter the starting and ending point between which prime numbers to be shown : 
0 10
The prime numbers between 0 and 10 are :2 3 5 7 

Enter the starting and ending point between which prime numbers to be shown : 
1 30
The prime numbers between 1 and 30 are :2 3 5 7 11 13 17 19 23 29 

Enter the starting and ending point between which prime numbers to be shown : 
37 100
The prime numbers between 37 and 100 are :37 41 43 47 53 59 61 67 71 73 79 83 89 97 

No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.