Friday, October 31, 2014

Java Binary Pro



Java program to generate random number

import java.util.*;
class GenerateRandomNumber
{
public static void main(String[] args) 
{
      int counter;
      Random rnum = new Random();
      System.out.println("Random Numbers:");
      System.out.println("***************");
      for (counter = 1; counter <= 5; counter++) 
      {
         System.out.println(rnum.nextInt(200));
      }
   }
}

Java Program to display first n or first 100 prime numbers

import java.util.Scanner;
          class PrimeNumberDemo
{
public static void main(String args[])
{
      int n;
      int status = 1;
      int num = 3;
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter the value of n:");
      n = scanner.nextInt();
      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are:");
         System.out.println(2);
      }
                for ( int i = 2 ; i <=n ;  )
      {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            i++;
         }
         status = 1;
         num++;
      }         
   }
}

Program to display first 100 prime numbers

class PrimeNumberDemo
{
 public static void main(String args[])
{
      int n;
      int status = 1;
      int num = 3;
      System.out.println("First 100 prime numbers are:");   
      System.out.println(2);
      for ( int i = 2 ; i <=100 ;  )
      {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            i++;
         }
         status = 1;
         num++;
      }         
   }
}

Java program to display prime numbers from 1 to 100 and 1 to n

class PrimeNumbers
{
 public static void main (String[] args)
 {                        
       int i =0;
       int num =0;
       String  primeNumbers = "";
 
       for (i = 1; i <= 100; i++)         
       {                                
          int counter=0;         
          for(num =i; num>=1; num--)
          {
             if(i%num==0)
             {
               counter = counter + 1;
        }
     }
     if (counter ==2)
     {
         primeNumbers = primeNumbers + i + " ";
     }        
       }      
       System.out.println("Prime numbers from 1 to 100 are :");
       System.out.println(primeNumbers);
   }
}

Java program to check prime number

import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
          {             
   int temp;
   boolean isPrime=true;
   Scanner scan= new Scanner(System.in);
   System.out.println("Enter a number for check:");
   int num=scan.nextInt();
   for(int i=2;i<=num/2;i++)
   {
           temp=num%i;
      if(temp==0)
      {
         isPrime=false;
         break;
      }
   }
   if(isPrime)
      System.out.println(num + " is Prime Number");
   else
      System.out.println(num + " is not Prime Number");
   }
}


No comments:

Post a Comment