Saturday, 10 March 2012

Program to generate Prime numbers.

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
package com.kota.java;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class PrimeNumber {
 public static void main(String[] args) throws Exception{
    int i;
    BufferedReader bf = new BufferedReader(
    new InputStreamReader(System.in));
    System.out.println("Enter number:");
    int num = Integer.parseInt(bf.readLine());
    System.out.println("Prime number: ");
    for (i=1; i < num; i++ ){
    int j;
    for (j=2; j<i; j++){
    int n = i%j;
    if (n==0){
    break;
    }
    }
    if(i == j){
    System.out.print("  "+i);
    }
    }
    }

}
/*
 *  0utput :
 *  --------
 *  Enter number:100
 Prime number: 
  2  3  5  7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97
 * */
 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...