Monday, 2 January 2012

java program to print prime numbers

package com.usr;

import java.util.Scanner;

public class PrimeNumbers {

   
    public static void main(String[] args) {
       
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter the number upto where ypu need to print prime numbers:");
          int number=sc.nextInt();
          int i,j,k;
          for(i=1;i<number;i++){
              for(j=2;j<i;j++){
                  k=i%j;
                  if(k==0){
                      break;
                  }
              }
              if(i==j){
                  System.out.println(""+i);
              }
          }
        
    }

}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.