Thursday 30 April 2015

Print the right angle triangle from the right side using number

package star;

import java.util.Scanner;

public class num2 {

public static void main(String[] args) {
int n, c, d,e;
     Scanner in = new Scanner(System.in);

     System.out.println("Enter the number of rows of triangle you want");
     n = in.nextInt();

     System.out.println("Triangle :-");

     for(c=1;c<=n;c++)
       {
           for(d=1;d<n+1-c;d++)
           {
               System.out.print(" ");
           }
           for(e=1;e<=c;e++)
           {
           System.out.print(""+e);
           }
       System.out.print("\n");
   }

}
}




Output:-

Enter the number of rows of triangle you want
4
Triangle :-
      1
    12
  123
1234

Wednesday 29 April 2015

Print the mirror image of right angle triangle using number

package star;

import java.util.Scanner;

public class num3 {

public static void main(String[] args) {
int n, c, d;
     Scanner in = new Scanner(System.in);

     System.out.println("Enter the number of rows of triangle you want");
     n = in.nextInt();

     System.out.println("Triangle :-");

     for(c=1;c<=n;c++)
       {
           for(d=1;d<=n+1-c;d++)
           {
               System.out.print(""+d);
           }
           System.out.print("\n");
       }
   }

}

Tuesday 28 April 2015

Right angle triangle using the number

package star;

import java.util.Scanner;

public class num1 {

public static void main(String[] args) {
int n, c, d;
     Scanner in = new Scanner(System.in);

     System.out.println("Enter the number of rows of triangle you want");
     n = in.nextInt();

     System.out.println("Triangle :-");

     for ( c = 1 ; c <= n ; c++ )
     {
        for ( d = 1 ; d <= c ; d++ )
        {
           System.out.print(" "+d);
         
        }

        System.out.println();
     }
  }
}

Monday 27 April 2015

Print the Pyramid in the numeric form

package star;

public class pyramidnum {


public static void main(String[] args) {

for (int i = 1; i <= 4; i++)
{
       for (int j = 4; j > i; j--)
       {
           System.out.print("  ");
       }
       for (int j = i; j > 1; j--)
       {
           System.out.print(j + " ");
       }
       for (int j = 1; j <= i; j++)
       {
           System.out.print(j + " ");
       }
       System.out.println();
   }

}

}

Saturday 25 April 2015

Print the mirror image of pyramid form

package star;

public class revpyramid {


public static void main(String[] args) {
int i,j,k;
   for(i=0;i<=4;i++)
   {
      for(j=0;j<i;j++)
      {
          System.out.print(" ");
      }
      for(k=0;k<4-i;k++)
      {
          System.out.print("*");
      }
      for(j=0;j<=4-i;j++)
           {
               System.out.print("*");
           }
           System.out.print("\n");
       }
 
         

}

}

Friday 24 April 2015

Print the star in the pyramid form

package star;

public class pyramid {


public static void main(String[] args) {
int i,j,k,l;
       for(i=0;i<4;i++)
       {
           for(j=1;j<4-i;j++)
           {
               System.out.print(" ");
           }
           for(k=0;k<=i;k++)
           {
           System.out.print("*");
           }
           for(l=1;l<=i;l++)
           {
               System.out.print("*");
           }

       System.out.print("\n");
   }
   }

}

Thursday 23 April 2015

Print the stars in the square form

package star;

public class Squarestars {


public static void main(String[] args) {

int i;
   for(i=1;i<=16;i++)
   {
      System.out.print("*");
      if(i%4==0)
      {
          System.out.print("\n");
      }
   }

}
}

Wednesday 22 April 2015

Mirror image of right angle triangle print from right side

package star;

public class stars4 {


public static void main(String[] args) {
int i,j,k;
   for(i=0;i<4;i++)
   {
      for(j=0;j<i;j++)
      {
          System.out.print(" ");
      }
      for(k=0;k<4-i;k++)
      {
          System.out.print("*");
      }
      System.out.print("\n");
   }
   }


}

Tuesday 21 April 2015

Print the star in the form of right angle triangle from right side

package star;

public class stars2 {


public static void main(String[] args) {
int i,j,k;
        for(i=0;i<5;i++)
        {
            for(j=1;j<5-i;j++)
            {
                System.out.print(" ");
            }
            for(k=0;k<=i;k++)
            {
            System.out.print("*");
            }
        System.out.print("\n");
    }

}
}

Friday 17 April 2015

Mirror Image of Right Angle Triangle

package star;

public class stars2 {


public static void main(String[] args) {
int i,j;
        for(i=1;i<=4;i++)
        {
            for(j=0;j<=4-i;j++)
            {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }

}

Thursday 16 April 2015

Print the star in a right angle triangle

package star;

public class stars1 {


public static void main(String[] args) {
int row, stars;
   
     for ( row = 1 ; row <= 5 ; row++ )
     {
        for ( stars= 1 ; stars<= row ; stars++ )
        {
           System.out.print("*");
         
        }

        System.out.println();
     }
  }
}

Monday 13 April 2015

Inheritance in java

package inhrit;

import java.util.Scanner;

public class inhrit {


public static void main(String[] args) {


abc a=new abc();
        a.func1();
        a.func2();
        def d=new def();
        d.func1();
        d.func2();
        uvw u=new uvw();
        u.func1();
        u.func2();
        mno x=new mno();
        x.func1();
        x.func2();
     }
}
interface pqr
{
 void func1();
 void func2();
}



class abc implements pqr
{

public void func1()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a+b;
System.out.println("Addition is:"+c);


}

public void func2()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a*b;
System.out.println("Multiplication is:"+c);

}

}


class def implements pqr
{

public void func1()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a-b;
System.out.println("Substraction is:"+c);

}

public void func2()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
c=a/b;
System.out.println("Divide is:"+c);

}

}

class uvw implements pqr
{

public void func1()
{
int row, stars;
   
     for ( row = 1 ; row <= 5 ; row++ )
     {
        for ( stars= 1 ; stars<= row ; stars++ )
        {
           System.out.print("*");
         
        }

        System.out.println();
     }

}

public void func2()
{
int i,j;
        for(i=1;i<=5;i++)
        {
            for(j=0;j<=5-i;j++)
            {
                System.out.print("*");
            }
            System.out.print("\n");
        }

}

}

class mno implements pqr
{

public void func1()
{
int i,j,k;
        for(i=0;i<5;i++)
        {
            for(j=1;j<5-i;j++)
            {
                System.out.print(" ");
            }
            for(k=0;k<=i;k++)
            {
            System.out.print("*");
            }
        System.out.print("\n");
    }

}

public void func2()
{
int i,j,k;
   for(i=0;i<5;i++)
   {
      for(j=0;j<i;j++)
      {
          System.out.print(" ");
      }
      for(k=0;k<5-i;k++)
      {
          System.out.print("*");
      }
      System.out.print("\n");
   }

}

}

Find odd number in a array

package findod;

import java.util.Scanner;

public class fiod {


public static void main(String[] args) {
int i;
int a[]=new int [10];
Scanner sc =new Scanner(System.in);
System.out.println("Enter the array");

for(i=0; i<10; i++)
{
a[i]=sc.nextInt();
}

for(i=0; i<10; i++)
{
System.out.println(a[i]);
}
for(i=0; i<10; i++)
{
if(a[i]%2!=0)
           {
               System.out.print("Element is at location: ");
               System.out.println(i);
               System.out.print("Element is: ");
               System.out.println(a[i]);
           }

}

}

}

Friday 10 April 2015

Magic Square using java

package reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class magics {


public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\n\nEnter the size of the matrix : ");
        int n=Integer.parseInt(br.readLine());

       
        int A[][]=new int[n][n];
        int i,j,k,t;
       
     
        if(n%2!=0)
        {
            i=0;
            j = n/2;
            k = 1;
            while(k<=n*n)
            {
                A[i][j] = k++;
                i--;
                j++;

                if(i<0 && j>n-1)
                {
                    i = i+2;
                    j--;
                }

                if(i<0)
                    i = n-1;

                if(j>n-1)
                    j = 0;

                if(A[i][j]>0)
                {
                    i = i+2;
                    j--;
                }
           
            }
        }
       
       
        else
        {
            k = 1;
           
         
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                    {
                        A[i][j] = k++;
                    }
            }
           
            j = n-1;
           
            for(i=0; i<n/2; i++)
            {
               
                t = A[i][i];
                A[i][i] = A[j][j];
                A[j][j] = t;
               
               
                t = A[i][j];
                A[i][j] = A[j][i];
                A[j][i] = t;
               
                j--;
            }
        }

     
        System.out.println("The Magic Matrix of size "+n+"x"+n+" is:");
        for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                    {
                        System.out.print(A[i][j]+ "\t");
                    }
             System.out.println();
            }
     }
    }



Thursday 9 April 2015

Matrix addition program in java

package array;

import java.util.Scanner;

public class matadd {


public static void main(String[] args)
{
  Scanner s = new Scanner(System.in);
 
      System.out.print("Enter number of rows: ");
      int row = s.nextInt();
     
      System.out.print("Enter number of columns: ");
      int col = s.nextInt();
     
      int a [][]= new int[row][col];
      int b [][]= new int[row][col];
     
      System.out.println("Enter the first matrix");
     
      for (int i = 0; i < row; i++)
      {
          for (int j = 0; j < col; j++)
          {
              a[i][j] = s.nextInt();
          }
      }
     
      System.out.println("Enter the second matrix");
     
      for (int i = 0; i < row; i++)
      {
          for (int j = 0; j < col; j++)
          {
              b[i][j] = s.nextInt();
          }
      }
     
      int c [][]= new int[row][col];
     
      for (int i = 0; i < row; i++)
      {
          for (int j = 0; j < col; j++)
          {
              c[i][j] = a[i][j] + b[i][j];
          }
      }
     
      System.out.println("The sum of the two matrices is");
     
      for (int i = 0; i < row; i++)
      {
          for (int j = 0; j < col; j++)
          {
              System.out.print(c[i][j] + " ");
          }
          System.out.println();
      }
  }
}

Java Test Programs

package test;

public class Test
{


public static void main(String[] args)
{

    System.out.println("test.......");
}

}