Google+
Showing posts with label Java Programs. Show all posts
Showing posts with label Java Programs. Show all posts

Monday, December 30, 2013

Program to display CIRCULAR PRIME numbers..

This program is used to display the circular prime numbers within 100.

EXAMPLE:

 197,971,719..97,79..3..

PROGRAM :

package Blog;
/**
 * @author Raju
 */
public class cirprime {
   public static void main(String args[])
    {
        for(int number =1;number<100;number++){
             if(isCircularprime(number)){
              System.out.println(number);              
          }}
    }
    public static boolean isCircularprime(int number)
    {
        int i=1,count=0,length = (int)(Math.log10(number)+1);
        int temp = number;
        while(i<=length){
           if(isPrime(temp))
                    {
                      count++;  
                    }
           temp = rotate(number);
           i++;
        }
        if(count==length)
        {
            return true;
        }
        return false;
    }
    public static boolean isPrime(int number){
        for(int i=2; i<number; i++){
           if(number%i == 0){
               return false; //number is divisible so its not prime
           }
        }
        return true; //number is prime now
    }
    public static int rotate(int number)
    {
        int length = (int)(Math.log10(number)+1);       
        int rotate = number%10;
        rotate = rotate * (int) Math.pow(10, length-1);
        number = number/10;
        rotate = rotate + number;
        return rotate;
    }       

}


Saturday, December 28, 2013

Program for displaying EVEN / ODD , FIBONACCI numbers and to SWAP without using Temp.

 Java is used in a wide variety of computing platforms from embedded devices and mobile phones on the low end, to enterprise servers and supercomputers on the high end.

1) This program is used to display EVEN /ODD numbers within a range you specify.

EXAMPLE :

EVEN numbers : 0 2 4 6 8 10 12 14 16 18 20 ....
ODD numbers  : 1 3 5 7 9 11 13 15 17 19...

PROGRAM :

package Blog;
import java.util.Scanner;
/**
 * @author Arun
 */
public class evenodd {
    public static void main(String args[])
    {
        System.out.println("Enter the range till which EVEN and ODD number to be printed :");
        Scanner scn = new Scanner(System.in);
        int numb = scn.nextInt();
        System.out.println("EVEN numbers within range is :");
        for(int i=0;i<=numb;i+=2)
        {
            System.out.print(i+" ");
        }
        System.out.println();
        System.out.println("ODD numbers within range is :");
        for(int i=1;i<=numb;i+=2)
        {
            System.out.print(i+" ");
        }
    }    

}

2) This program is used to display FIBONACCI numbers within a range you specify.

EXAMPLE:

Fibonacci numbers : 0 1 1 2 3 5 8 13 ...

PROGRAM:

package Blog;
import java.util.Scanner;
/**
 * @author Arun
 */
public class fibonacci {
    public static void main(String [] args)
    {
        int i=0,temp,currval;
        Scanner scn = new Scanner(System.in);
        System.out.println("Enter the range till which fibonacci number to be displayed :");
        int fib = scn.nextInt();
        System.out.print(i+" ");
        temp=i;
        i++;
        currval=i;
        while(i<fib)
        {
         System.out.print(currval+" "); 
         currval = i + temp;
         temp = i;
         i = currval;                             
        }        
    }    
}

3) This program is used to SWAP two numbers WITHOUT use of TEMPORARY variable :

EXAMPLE :

Before Swapping : a = -10 , b = -20
After Swapping : a = -20,b=-10

PROGRAM :

package Blog;
import java.util.Scanner;
/**
 * @author Arun
 */
public class swap {
    public static void main(String args[])
    {
        Scanner scn = new Scanner(System.in);
        System.out.println("Enter number A :");
        int a = scn.nextInt();
        System.out.println("Enter number B :");
        int b= scn.nextInt();
        a=a+b;
        b=(a-b);
        a=(a-b);
        System.out.println("Value in A :"+a);
        System.out.println("Value in B :"+b);
    }
    
}

Friday, December 27, 2013

Program to Rotate a Number using JAVA

Java a platform independent programming language.In this program you can rotate the number using Java.

EXAMPLE :
Original number:54321
Rotated Number :15432

PROGRAM :
import java.util.Scanner;
/**
 * @author Arun
 */
public class rotatetest {
    public static void main(String args[])
    {
        Scanner scn = new Scanner(System.in);
        System.out.println("Enter the Number to rotate");
        int number = scn.nextInt();
        int length = (int)(Math.log10(number)+1);       
        int rotate = number%10;
        rotate = rotate * (int) Math.pow(10, length-1);
        number = number/10;
        rotate = rotate + number;
        System.out.println("Rotated numb is "+rotate);
    }
}

How to reverse only words in a string in place using java ??

In this program you can reverse the words in a string without changing the order of placement of words using JAVA.

Example : -
original string : emoclew  ot   dehsaelnu-ofni  !! 
Reversed output : welcome to info-unleashed !!

Program :

package test;
import java.util.Scanner;
/**
 * @author Arun
 */
public class strrev {
   String original , reverse="";
   int i=0,t=0;
   void test()
   {   
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a string to reverse : ");
    original = in.nextLine();
    int length = original.length();
    for (;i <length ;i++ ){
     if(Character.isWhitespace(original.charAt(i))||i == length-1)
       {
        disp(i);
       }           
     }
     System.out.println();
   }
   void disp(int l)
    {       
      for(int j = i;j>=t;j--)
       {
         reverse = reverse+original.charAt(j);
       }
      System.out.print(reverse);
      t=i;
      reverse =" ";
    }
   public static void main(String args[])
    {
        strrev s = new strrev();
        s.test();
    }
}