Friday, June 8, 2012

Write a program to display al?l prime numbers between 1 to 1000.


♣♣♣ WRITE A PROGRAM TO DISPLAY ALL PRIME NUMBERS BETWEEN 1 TO 1000. ♣♣♣


import java.io.*;
class pro5
{
            public static void main(String args[]) throws IOException
            {
            int num,cnt,last,flg;
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the last number of range: ");
            last=Integer.parseInt(b.readLine());
            System.out.print("List of all Prime Number upto "+last+" : \n");
                        for(num=1;num<=last;num++)
                        {
                        flg=1;
                                    for(cnt=2;cnt<=num/2;cnt++)
                                    {         
                                                if(num%cnt==0)
                                                {
                                                flg=0;
                                                break;
                                                }
                                    }
                        if(flg==1)
                        System.out.print(num+"   ");
                        }
            }
}

  

OUTPUT :-

            Enter the last number of range: 50

List of all Prime Number upto 50:

            1          2          3          5          7          11        13        17        19        23
           29         31        37       41       43          47

No comments:

Post a Comment