/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package primenumber;

/**
 *
 * @author mcmanuel
 */
public class PrimeNumber {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int num, a, b, c;
        num = 10;
        for(a = 1; a <= num; a++)
        {
            b = 0;
            for(c = 2; c <= a/2; c++)
            {
            if(a%c == 0)
            {
                b = b + 1;
            }
            if(c == 0)
            {
            System.out.println("The prime number is" + a);
            }
            }

        }

    }

}

Recommended Answers

All 5 Replies

What's the question?

want to write acode to print prime numbers

you have done several mistakes.
At line 28 you have matched c==0, but you have to match b==0,because you are incementing b value.

Also you have ambiguously closed 2nd for loop, it will be exited after checking it will be divisible by any no. less than that.

So change it.If there are any doubt then ask.

Thanks

 for(a = 1; a <= num; a++)
{
    b = 0;
    for(c = 2; c <= a/2; c++)
    {
        if(a%c == 0)
        {
            b = b + 1;
        }
    }
    if(b == 0)
    {
        System.out.println("The prime number is" + a);
    }
}

Make changes like this and It should print the numbers for you

nJoy!!!

No doing any homework for others. Explain to the OP where is wrong and why. Also, your code is extremely slow with bad algorithm even though it is taken directly from the OP. One more, encouraging beginners to ignore the meaning of variable name is ruining others. If these beginners come out and become developers/programmers, others will be suffered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.