Computing.Net > Forums > Programming > Prime no's pgm

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Prime no's pgm

Reply to Message Icon

Name: spani
Date: February 28, 2009 at 00:03:35 Pacific
OS: Windows XP
CPU/Ram: 2.00Ghz
Product: Hcl DESKTOP
Subcategory: Java
Comment:

//java pgm to print prime no's upto given integer

import java.util.Scanner;
class Primes
{
int c,n,s,i,j,x;
int k=0;
void getData()
{
Scanner ip=new Scanner(System.in);
System.out.println("Enter n value");
n=ip.nextInt();
System.out.println("Primes are "+2);
}
void makePrimes()
{
for(int i=2;i<=n;i++)
{
if(i%2!=0)
{
s=(int)Math.sqrt(i)+1;
for(int j=2;j<=s&&j!=i;j+=2)
{
c=i%j;
if(c==0)
{
k++;
break;
}
}
if(k==0)
display(i);
}
}
}
void display(int x)
{
System.out.println(x);
}
}
class PrimeNum
{
public static void main(String args[])
{
Primes A=new Primes();
A.getData();
A.makePrimes();
}
}


Plz tell me wats wrong with this pgm
in the o/p of the pgm it is showing the non primes as primes



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Need help with Unix Scrip... windows script



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Prime no's pgm

C++ program to add 2 bin. no's www.computing.net/answers/programming/c-program-to-add-2-bin-nos/3542.html

4 U Trip On Primes www.computing.net/answers/programming/4-u-trip-on-primes/4278.html

Prime number checking www.computing.net/answers/programming/prime-number-checking/6158.html