Tuesday, December 23, 2014

List odd even and prime numbers in java pro



List Even Numbers

public class ListEvenNumbers
{
public static void main(String[] args)
{
 int limit = 50;
System.out.println("Printing Even numbers between 1 and " + limit);
for(int i=1; i <= limit; i++)
{
 if( i % 2 == 0)
{
System.out.print(i + " ");
      }
    }
  }
}

List Odd Numbers

public class ListOddNumbers
{
public static void main(String[] args)
{
int limit = 50;
System.out.println("Printing Odd numbers between 1 and " + limit);
for(int i=1; i <= limit; i++)
{
if( i % 2 != 0)
{
System.out.print(i + " ");
           }
      }
   }
}

Prime Numbers

public class GeneratePrimeNumbersExample
{
public static void main(String[] args)
{
int limit = 100;
System.out.println("Prime numbers between 1 and " + limit);
for(int i=1; i < 100; i++)
{              
boolean isPrime = true;
for(int j=2; j < i ; j++)
{
if(i % j == 0)
{
isPrime = false;
break;
  }
}
if(isPrime)
System.out.print(i + " ");
     }
   }
}

No comments:

Post a Comment