Monday, 13 July 2015

Program to determine Prime Numbers from 1 to 100 (Core JAVA)

Save File as primeNumber.java



package myFirstProject;

public class primeNumber {

 public static void main(String[] args) {
  
   int a1 =0;
   int number =0;
   //Empty String for printing Prime Numbers
   String  primeNumbers = "";

        for (a1 = 1; a1 <= 100; a1++)         
        {        
           int counter=0;    
           for(number =a1; number>=1; number--)
           {
              if(a1%number==0)
              {
               counter = counter + 1;
              }
           }
            if (counter ==2)
            {
         //Appended the Prime number to the String
         primeNumbers = primeNumbers + a1 + " ";
            } 
        } 
         System.out.println("Prime Numbers from 1 to 100 are :");
         System.out.println(primeNumbers);
 }
}

No comments:

Post a Comment