14 September, 2012

Palindrome

Checks whether a given String is a Palindrome or not

 

import java.util.*;
public class palindrome
{
public static void main(String a[])
{
System.out.println("Enter any string");
Scanner s=new Scanner(System.in);
String str=s.next();

for(int i=0,j=str.length()-1;i<=j;i++,j--)
{
if(str.charAt(i)==str.charAt(j));
else
{
System.out.println("Not Palindrome : "+str);
System.exit(1);
}
}
System.out.println("Palindrome : "+str);
}
}

10 September, 2012

Sort the Name List

import java.util.*;
class Sortst
{
public static void main(String a[])
{
System.out.println("How many string u want to enter");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String st[]=new String[n+1];
for(int i=0;i<n;i++)
{
System.out.println("Enter string "+(i+1));
st[i]=s.next();
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(st[i].compareTo(st[j])>0)
{
String t=st[i];
st[i]=st[j];
st[j]=t;
}
}
}
System.out.println("Strings in order are :\n\n");
for(int i=0;i<n;i++)
System.out.println(st[i]);
}
}

07 September, 2012

Bubble Sort

import java.util.*;
class bubblesort
{

public static void main(String[] args)
{
int []a = {10,2,13,40,25,6,27,38,9} ;
bubblesort(a);
for(int i=0;i<=a.length-1;i++)
    System.out.println(a[i]);
}

public static void bubblesort(int[] a)
{
for(int i=0;i<=a.length-2;i++)
{
    for(int j=0;j<=a.length-2-i;j++)
        if(a[j+1]<a[j])
        {
            int t=a[j];
            a[j]=a[j+1];
            a[j+1]=t;
        }
}
}

}

Insertion Sort

import java.util.*;
class insertionsort
{

public static void main(String[] args)
{
int []a = {5,25,12,28,9,12,15,13,10,7} ;
insertionsort(a);
for(int i=0;i<=a.length-1;i++)
    System.out.println(a[i]);
}

public static void insertionsort(int[] a)
{
 for(int j=1;j<a.length;j++)
 {
  int key=a[j];
  int i;
  for(i=j-1;i>=0&&a[i]>key;i--)
     a[i+1]=a[i];
  a[i+1]=key;
 }
}

}

Selection Sort

import java.util.*;
class selectionsort
{

public static void main(String[] args)
{
int []a = {15,25,2,8,19,22,5,30,0,7} ;
selectionsort(a);
for(int i=0;i<=a.length-1;i++)
    System.out.println(a[i]);
}

public static void selectionsort(int[] a)
{
for(int i=0;i<=a.length-2;i++)
{
    int pos=i;
    for(int j=i+1;j<=a.length-1;j++)
        if(a[j]<a[pos])
            pos=j;
            int t=a[pos];
            a[pos]=a[i];
            a[i]=t;
}
}

}

Matrix Multiplication

import java.util.*;
public class Matrixmult
{
public static void main (String [] args)
{

int [] [] a ={ {1,2,3} , {4,5,6} , {7,8,9} };
int [] [] b ={ {3,3,3} , {2,2,2} , {1,1,1} };
int m=3, n=3 , r=3;
int [] [] c = new int [m][r];

for (int i=0; i<=m-1; i++)
{       for(int j=0; j<=n-1; j++)
    for(int k=0;k<=r-1;k++)
     c[i][k] = c[i][k]+a[i][j]*b[j][k];
}

for (int i=0; i<=m-1; i++)
{       for(int j=0; j<=n-1; j++)
    System.out.printf("%d\t",c[i][j]);
      System.out.println();
}

}
}

06 September, 2012

Age Validation.

import java.util.*;
public class AgeException
{
public static void main (String [] args)
{
Scanner s = new Scanner(System.in);
while(true)
{
try
{
System.out.println("Enter ur Age");
int n=s.nextInt();
int x=age(n);
return;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public static int age(int n)throws Exception
{
if (n<18 || n>60)
throw new Exception ("Please Enter age18 to 60");
System.out.println("You r selected");
return n;
}
}

Use StringTokenizer class

Java program that displays input integer and the sum of all the integers 

( use StringTokenizer class )



import java.io.*;
import java.lang.*;
import java.util.*;
class StringTokenizerExample
{
  public static void main(String args[])
 {
  int n;
  int sum=0;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter integer seperated by space");
  String str=s.nextLine();
  StringTokenizer st=new StringTokenizer(str," ");
  while(st.hasMoreTokens())
  {
    String temp=st.nextToken();
    n=Integer.parseInt(temp);
    System.out.println(n);
    sum=sum+n;
   }
    System.out.println("sum="+sum);
 }
}

All real solutions to the quadratic equation

Java program that prints all real solutions to the quadratic equation ax2+bx+c=0.


import java.lang.*;
import java.util.*;
class Quadratic
{
public static void main(String args[])
{
int a,b,c,d;
double e,f;

Scanner s = new Scanner(System.in);

System.out.println("Enter value a = ");
a=s.nextInt();
System.out.println("Enter value b = ");
b=s.nextInt();
System.out.println("Enter value c = ");
c=s.nextInt();
System.out.println("Your equation : "+a+"x2 + "+b+"x + "+c+" = 0");

d=(b*b)-(4*a*c);
if(d<0)
{
    System.out.println("No real roots");
}
else
{
    e=((-b+Math.sqrt(d))/(2*a));
    f=((-b-Math.sqrt(d))/(2*a));
    System.out.println("Roots are :"+e+" , "+f);
}

}
}

Prints out all prime numbers upto that integer

Program that prints out all prime numbers upto that integer


import java.io.*;
import java.lang.*;
import java.util.*;
class Prime
{
  public static void main(String args[])
  {
    int i,j,n;
    int count=0;
    Scanner s=new Scanner(System.in);
    System.out.println("Enter Number : ");
    n=s.nextInt();
    for(i=1;i<=n;i++)
    {
      count=0;
      for(j=1;j<=i;j++)
      {
        if(i%j==0)
         count++;
      }
      if(count==2)
      System.out.println("\t"+i);
     }
   }
}


05 September, 2012

Fibonacci by Recursive Functions

Write a java program that uses recursive functions

import java.lang.*;
import java.util.*;
class FibonacciRec
{
   public static void main(String args[])
   {
      int a=0,b=1;
      Recurssion fr= new Recurssion();
      Scanner s = new Scanner(System.in);
      System.out.println("Enter length of series");     

      int n1= s.nextInt();
      int n=n1-3;
      System.out.println("Fibonacci series for "+n1+" is");
      System.out.print(" "+a+" "+b+" ");
      fr.recurssion(n,a,b);
   }
}
class Recurssion
{
  int i=0;
  public void recurssion(int n,int a ,int b)
  {
      int c;
      if(i<=n)
      {
        c=a+b;
        a=b;
        b=c;
        System.out.print(c+" ");
        i++;
        recurssion(n,a,b);
      }
   }
}