Forum : to find prime numbers
Brief description  about Online courses   join in Online courses
View Harini  J 's Profile

to find prime numbers

I know the concept to find prime numbers but its giving error please help me out this is code i have coded for the following question but its not correct.

class Prime
{
public static void main(String[] args)
{
int[] num={12,34,35,42,9,76,31,54,99,150};
int i,j,c=0;
for(i=0;i<10;i )
{
for(j=1;j<=num[i];j )
{
if(num[i]%j==0)
{
c=c 1;
}


}
if(c==2)
System.out.println("prime number is :" num[i]);
else
System.out.println("no prime number");
}


}
}

please teach me correct code and help me out by giving the solution to the following programme.

1. Consider an array with ten elements 12,34,35,42,9,76,31,54,99,150. Please write a program to find the prime number in the array and to print the number and it's index position in the array.
Hint: Use the example program as reference.
Asked by Harini J | Jul 8, 2010 |  Reply now
Replies (6)
View harini j 's Profile
Thank You so much its working
Jul 16, 2010
View harini j 's Profile
Thank u so much.Its working.
Jul 16, 2010
View bhushan hanumant shinde 's Profile
i guess this might help u with the clean human readable code....

public class FindIstPrimeInArray {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {12,34,35,42,9,76,31,54,99,150};

boolean primeNumberFound;
for(int i = 0; i < array.length; ++i) {
primeNumberFound = true;
for(int j = 2; j < array[i]; ++j) {
if(array[i] % j != 0)
continue; //use of continue.
primeNumberFound = false;

}
if(primeNumberFound) {
System.out.println("Prime Number " + array[i]+ " found at index :" + i);
}
}
}

}
Jul 12, 2010
View gurpreet kohli 's Profile
anytime dear!!
Jul 11, 2010
View harini j 's Profile
Thank you so much its working.
Jul 10, 2010
View gurpreet kohli 's Profile
hi harini.. i tried somthing else here for u here....
class Prime
{
public static void main(String[] args)
{
int[] num={12,34,35,42,9,76,31,54,99,150};
for(int i=0;i<num.length;i++ )
{
int j;
for( j=2;j<num[i];j++ )
{
int c=num[i]%j;
if(c==0)
{
System.out.println(" the number "+num[i]+"is not prime");
break;
}}
if(j==num[i])
System.out.println(" the number"+num[i]+" is prime");

}}}
Jul 10, 2010