Prime Number

View previous topic View next topic Go down

Prime Number

Post by Sujith on Tue Jul 06, 2010 8:32 pm

Code:

/* Program to print prime numbers from 1 to 10*/
class PrimeNumber
{
    public static void prime(int n)
    {
        int count=0;
        /* This checks the number of times
          * the number gets completely divided
          */
        for(int i=2;i<n;i++)
        {
              if(n%i==0)
              {
                  count++;
              }
        }
        if(count==0)
        {
            System.out.print(n+" ");
        }
    }
    public static void main (String[] args)
    {
        for(int a=1;a<=10;a++)
        {
            prime(a);  // Calling prime() function
        }
    }
}

Variable Table


Variable NameData TypeDescription
count
int
Checks the number of times the number gets completely divided
i
int
Used to iterate the for loop
n
int
Formal Parameter
a
int
Actual Parameter

______________________________________________________
It is the simplest things that are most Complicated!
Been on this forum since 2639 days!

avatar
Sujith
LSF Expert
LSF Expert

Posts : 126

http://osalfps.org

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum