PROGRAMMING IN JAVA
SOME OF USEFUL PROGRAMS WRITTEN IN JAVA
To Find a palindrome
import java.io.*;
class pravikravi
{
int n,rev=0,r;
void find()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a numbers");
n=Integer.parseInt(br.readLine());
int a=n;
while(n>0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
if(a==rev)
System.out.println("The number is palindrome");
else
System.out.println("The number is not palindrome");
}
}
TRIBINOCCI
class PRAVIKRAVI
{
int n,a=0,b=1,c=2,d;
void find()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a numbers");
n=Integer.parseInt(br.readLine());
System.out.print(a+"\t"+b+"\t"+c+"\t");
for(int i=3;i<=n;i++)
{
d=a+b+c;
a=b;
b=c;
c=d;
System.out.print(d+"\t");
}
}
}
USING STRING
BIGGEST AND SMALLEST STRING
import java.io.*;
import java.lang.*;
class array
{
int n,big,sbig,r1,g2,c1,n2,t,k,i,j;
int a[][] = new int[5][4];
int c[]=new int[20];
int d[][]=new int[20][20];
void find()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter array diamention");
n=Integer.parseInt(br.readLine());
System.out.println("Enter array Elements");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("Original Metrix is");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print( a[i][j]+" ");
}
System.out.println(" ");
}
big=a[0][0];
sbig=big;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>big)
{
sbig=big;
big=a[i][j];
r1=i+1;
c1=j+1;
}
else
if(a[i][j]>sbig)
{
sbig=a[i][j];
g2=i+1;
n2=j+1;
}
}
}
System.out.println("Big is "+big);
System.out.println("Second Big is "+sbig);
System.out.println("row and colomn position of largest element is "+r1+" "+c1);
System.out.println("row and colomn position of second largest element is "+g2+" "+n2);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
c[k]=a[i][j];
k++;
}
}
System.out.println("Array changed to Single dimension is");
for(int k=0;k<n*n;k++)
{
System.out.print(" "+c[k]);
System.out.println(" ");
}
for(int i=1;i<k;i++)
{
for(int j=0;j<k-i;j++)
{
if(c[j] > c[j+1])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
}
}
System.out.println("Sorted Array is ");
for(int i=0;i<k;i++)
{
System.out.println( c[i]+" ");
}
for(int f=0;f<k+1;f++)
{
for(int j=0;j<k;j++)
{
d[i][j]=c[f];
k--;
f++;
}
}
System.out.println("Sorted Array in matrix form ");
for(int i=0;i<k;i++)
{
for(int j=0;j<k;j++)
{
System.out.print( d[i][j]+" ");
}
System.out.println( " ");
}
}
}
RECURSION
import java.io.*;
class Recursion
{
int a,b,c,limit;//variable declaration
public Recursion()
{//Constructer to assign initial values
a=0;b=1;c=1;
}//Closing method
void input()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//Accepting value of limit
System.out.println("Enter the limit");
limit=Integer.parseInt(br.readLine());
}//Closing method
int fib(int n)
{//To return nth fibbonacci term using recursion
if(n<=1)
return n;
else
return fib(n-1)+fib(n-2);
}//Closing method
void generate_fibseries()
{//To generate fibonacci series
System.out.println("THE FIBONACCI SERIES UPTO "+limit+" TERMS IS:");
System.out.println(a);
System.out.println(b);
for(int i=2;i<limit;i++)
{
c=fib(i);
System.out.println(c);
}
}//Closing method
}//closing class
MORE THAN 20 PROGRAMS WILL BE PUBLISHED SOON......