Monday, 27 June 2016

Program to Check whether a String is Positive or not.

Explanation :
ANT is a positive String (Since T comes after N and N comes after A)
APPLE is not positive since L comes before P in the alphabetical order.

Code:

import java.util.Scanner;
public class PositiveString
{
    char a1,b;
    boolean checkPositive(String a)
    {
        System.out.println("Given input is :"+a);
        int f,s;
        for(int i=0;i<a.length()-1;i++)
        {
            a1=a.charAt(i);
            b=a.charAt(i+1);
            f=Character.getNumericValue(a1);
            s=Character.getNumericValue(b);
            if(f-s>0)
                return false;
            else
                continue ;
        }
        return true;
        }
   
    public static void main(String[] args)
    {
        PositiveString n=new PositiveString();
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the string:");
       
        String s=sc.next();
        boolean a=n.checkPositive(s);
        if(a)    System.out.println("positive");
        else    System.out.println("Negative");
        sc.close();
    }     
}

Program to Convert a String to an Encrypted Code.

Explanation :
For String =cde the Encrypted Code Shall be ASCII Values of c+9, d+9, e+9.
If n=abc then Output will be "jkl"
If n=az then Output will be "ji".

Code:

import java.util.Scanner;
public class Encryption
{
    String encryptString(String st)
    {
        char ch[]=st.toCharArray();
        for(int i=0;i<st.length();i++)
        {
            if(ch[i]>='a' && ch[i]<='q')
                  ch[i]=(char) (ch[i]+9);
            else
                  ch[i]=(char) (ch[i]-17); //(26-9)
        }
        String str=new String(ch);
        return str;
    }
   
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String str1,str2;
        System.out.println("Enter String");
        str1=sc.next();
        Encryption ecr=new Encryption();
        str2=ecr.encryptString(str1);
        System.out.println("Encrypted string is  :"+str2);
        sc.close();
    }
}

Program to perform Sum of Digits On Double Datatype.

Explanation :
If n=123.56 then Output will be 6:11 .
If n=401 then Output will be 5:0 .

Code:

import java.util.Scanner;
public class DoubleSumOfDigits
{
    String getSum(double d)
    {
        String s=Double.toString(d);
        int res1=0,res2=0;
        int i=s.indexOf(".");
       
        for(int j=0;j<i;j++)
        {
            int n=s.charAt(j);
            res1+=Character.getNumericValue(n);
        }
        for(int j=i+1;j<s.length();j++)
        {
            res2+=Character.getNumericValue(s.charAt(j));
        }
        String fnl=Integer.toString(res1)+":"+Integer.toString(res2);
        return fnl;
    }
   
    public static void main(String[] args)
    {
       
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any double number");
       
        DoubleSumOfDigits das=new DoubleSumOfDigits();
        String b=das.getSum(sc.nextDouble());
        System.out.println(b);

        sc.close();
    }
}

Program to Print Numbers in String Format .

Explanation :
If n=123 then Output will be "One Two Three" .

Code:

import java.util.Scanner;
public class NumberToString
{
    String getNumber(int num)
    {
        String str="";
        int d;
        while(num!=0)
        {
            d=num%10;
            switch(d)
            {
               case 0:str="Zero  "+str;            break;
               case 1:str="One  "+str;             break;
               case 2:str="Two  "+str;            break;
               case 3:str="Three  "+str;          break;
               case 4:str="Four  "+str;            break;
               case 5:str="Five  "+str;            break;
               case 6:str="Six  "+str;              break;
               case 7:str="Seven  "+str;         break;
               case 8:str="Eight  "+str;          break;
               case 9:str="Nine  "+str;           break;
               default:break;
            }
            num=num/10;
        }
        return str;
    }

    public static void main(String args[])
    {
        NumberToString cnt= new NumberToString();
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter number  :");
        int num=sc.nextInt();
        String str=cnt.getNumber(num);
        System.out.println(num+" in String format is: "+str);
        sc.close();
    }
}

Thursday, 23 June 2016

Program to print a Star Pattern Type-7.

Explanation :
If n=6 then output will be
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *  * .

Code:

import java.util.Scanner;
public class StarPattern7
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j;
        for (i=0; i<line; i++)
        {
            for (j=0; j<line; j++)
            {
                System.out.printf(" * ");
            }
            System.out.printf("\n");
        }
    }
}

Program to print a Star Pattern Type-6.

Explanation :
If n=6 then output will be
 *
 *  *
 *  *  *
 *  *  *  *
 *  *  *  *  *
 *  *  *  *  *  * .

Code:

import java.util.Scanner;
public class StarPattern6
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0;
        for (i=0; i<line; i++)
        {
            for (j=0; j<=i; j++)
            {
                System.out.printf(" * ");
            }
            System.out.printf("\n");
        }
    }
}

Program to print a Star Pattern Type-5.

Explanation :
If n=6 then output will be
           *
         **
       ***
     ****
   *****
 ******  .

Code:

import java.util.Scanner;
public class StarPattern5
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0,samp=1;
        for (i=1; i<=line; i++)
       {
            for (j=line; j>=i; j--)
           {
                System.out.printf(" ");
            }
            for (k=1; k<=i; k++)
           {
                System.out.printf("*");
            }
            System.out.printf("\n");
        }
    }
}
 

Program to print a Star Pattern Type-4.

Explanation :
If n=6 then output will be
  ******
    *****
      ****
        ***
          **
            *.

Code:

import java.util.Scanner;
public class StarPattern4
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0,samp=1;
        for (i=line; i>=1; i--)
        {
            for (k=samp; k>=0; k--)
            {
                System.out.printf(" ");
               
            }
            for (j=i; j>=1; j--)
            {
                System.out.printf("*");
            }
            samp = samp + 1;
            System.out.printf("\n");
        }
    }
 }

Program to print a Star Pattern Type-3.

Explanation :
If n=6 then output will be
 *  *  *  *  *  *
 *  *  *  *  *
 *  *  *  *
 *  *  *
 *  *
 * .

Code:

import java.util.Scanner;
public class StarPattern3
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0;
        for (i=line; i>=1; i--)
        {
            for (j=1; j<=i; j++)
            {
                System.out.printf(" * ");
            }
            System.out.printf("\n");
        }
    }
 }

Program to print a Star Pattern Type-2.

Explanation :
If n=4 then output will be
       *
     *  *
   *  *  *
 *  *  *  * .

Code:

import java.util.Scanner;
public class StarPattern2
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,t=0;
        for (i=1; i<=line; i++)
        {
            for (k=t; k<line; k++)
            {
                System.out.printf(" ");
            }
            for (j=0; j< i; j++)
            {
                System.out.printf(" * ");
                t = t + 1;
            }
            System.out.printf("\n");
        }
    }
 }

Program to print a Star Pattern Type-1.

Explanation :
If n=6 then output will be
           *
         **
       ***
     ****
   *****
 ******
   *****
     ****
       ***
         **
           *.

Code:

import java.util.Scanner;
public class StarPattern1
{
    public static void main( String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter the no. of lines: ");
        int line=sc.nextInt();
     
        int i,j,k,samp=1;
        for (i=1; i<=line; i++)
        {
            for (k=samp; k<=line; k++)
            {
                System.out.printf(" ");
            }
            for (j=0; j< i; j++)
            {
                System.out.printf("*");
            }
            samp = samp + 1;
            System.out.printf("\n");
        }
        samp = 1;
        for (i=line-1; i>=1; i--)
        {
            for (k=samp; k>=0; k--)
            {
                System.out.printf(" ");
            }
            for (j=i; j>=1; j--)
            {
                System.out.printf("*");
            }
            samp = samp + 1;
            System.out.printf("\n");
        }
    }
}

Wednesday, 22 June 2016

Program to print a Number Pattern Type-28(Pascal Triangle).

Explanation :
If n =6 then output will be
              1
            1  1
          1  2  1
        1  3  3  1
     1  4   6   4  1
    1 5 10 10  5  1.

Code:

import java.util.Scanner;
public class Pattern28
{
    public static void main( String args[])
    {
        int line,i,j;
        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter the no. of lines: ");
        line=sc.nextInt();
       
    
        for(i=0;i<line;i++){
             for(j=0;j<line-i-1;j++)
                 System.out.printf(" ");
    
             for(j=0;j<=i;j++)
                 System.out.print(fact(i)/(fact(j)*fact(i-j)));
             System.out.printf("\n");
        }
    }
    
   public static long fact(int num){
        long f=1;
        int i=1;
        while(i<=num){
             f=f*i;
             i++;
      }
      return f;
     }
}

Program to print a Number Pattern Type-27.

Explanation :
If n=6 then output will be
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36.

Code:

import java.util.Scanner;
public class Pattern27
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d ",i*j);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-26.

If n=5 then output will be
11111
0000
111
00
1 .

Code:

import java.util.Scanner;
public class Pattern26
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i, j;
        for (i=n;i>=1;i--)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d",i%2);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-25.

Explanation :
If n=5 then output will be
1
10
101
1010
10101.

Code:

import java.util.Scanner;
public class Pattern25
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d",j%2);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-24.

Explanation :
If n=5 then output will be
                       1
                   2  3
               4  5  6
          7  8  9 10
 11 12 13 14 15 .

Code:

import java.util.Scanner;
public class Pattern24
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        k=1;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=1;j--)
            {
                if(j > i)
                        System.out.printf("   ");
                else
                        System.out.printf("%3d",k++);
            }
            System.out.printf("\n");
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-23.

Explanation :
If n=5 then output will be
  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15 .

Code:

import java.util.Scanner;
public class Pattern23
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        k=1;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=i;j--)
            {
                System.out.printf("%3d",k++);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-22.

Explanation :
If n=5 then output will be
 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15  .

Code:

import java.util.Scanner;
public class Pattern22
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=1;
         for(int i = 1 ; i <= n ; i++)
         {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(" "+k++);
             }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-21.

Explanation :
If n=6 then output will be
            1
          123
        12345
      1234567
    123456789
1234567891011
    123456789
     1234567
       12345
         123
           1  .

Code:

import java.util.Scanner;
public class Pattern21
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i, j, k;
        for (i=1;i<=n;i++)
        {
            for (j=i;j<n;j++)
            {
                System.out.printf(" ");
            }
            for (k=1;k<(i*2);k++)
            {
                System.out.printf("%d",k);
            }
            System.out.printf("\n");
        }
        for (i=n-1;i>=1;i--)
        {
            for (j=n;j>i;j--)
            {
                System.out.printf(" ");
            }
            for (k=1;k<(i*2);k++)
            {
                System.out.printf("%d",k);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-20.

Explanation :
If n=6 then output will be
                         1  
                     1  2  3
                1  2  3  4  5
            1  2  3  4  5  6  7
        1  2  3  4  5  6  7  8  9
 1  2  3  4  5  6  7  8  9  10  11  .

Code:

import java.util.Scanner;
public class Pattern20
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1; i<=n; i++)
        {
            for (j=1; j<=n-i; j++)
            {
                System.out.printf("   ");
            }
            for (k=1; k<=2*i-1; k++)
            {
                System.out.printf(" %d ",k);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-19.

Explanation :
If n=6 then output will be
          1
        1 2
      1 2 3
    1 2 3 4
  1 2 3 4 5
1 2 3 4 5 6 .

Code:

import java.util.Scanner;
public class Pattern19
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>i;j--)
            {
                System.out.printf(" ");
            }
            for (j=1;j<=i;j++)
            {
                System.out.printf("%d ",j);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-18.

Explanation :
If n=6 then output will be
        1
      2 2
     3 3 3
    4 4 4 4
   5 5 5 5 5
 6 6 6 6 6 6 .

Code:

import java.util.Scanner;
public class Pattern12
{
    public static void main(String args[])
    {
        System.out.print("Enter the number of Rows:");
        Scanner sc=new Scanner(System.in);
        int z=sc.nextInt();
        int i, k, x,j=5;
        for (i=1;i<=z;i++)
        {
            for (k=1;k<=j;k++)
            {
                System.out.printf(" ");
            }
            for (x=1;x<=i;x++)
            {
                System.out.print(i);
                System.out.printf(" ");
            }
            System.out.printf("\n");
            j=j-1;
        }
    }
}

Program to print a Number Pattern Type-17.

Explanation :
If n=6 then output will be
111111
22222
3333
444
55
6.

Code:

import java.util.Scanner;
public class Pattern17
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i, j;
        for (i=1;i<=n;i++)
        {
            for (j=n;j>=i;j--)
            {
                System.out.printf("%d",i);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-16.

Explanation :
If n=6 then output will be
1
22
333
4444
55555
666666.

Code:

import java.util.Scanner;
public class Pattern16
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-15.

Explanation :
If n=6 then output will be
654321
65432
6543
654
65
6.

Code:

import java.util.Scanner;
public class Pattern15
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = n; j >= i ; j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-14.

Explanation :
If n=6 then output will be
654321
54321
4321
321
21
1.

Code:

import java.util.Scanner;
public class Pattern14
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = n ; i >= 1 ; i--)
        {
            for(int j = i; j >= 1 ; j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-13.

Explanation :
If n=6 then output will be
1
23
345
4567
56789
67891011.

Code:

import java.util.Scanner;
public class Pattern13
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1;i<=n;i++)
        {
            j=i;
            for (k=1;k<=i;k++)
            {
                System.out.printf("%d",j++);
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-12.

Explanation :
If n=6 then output will be
1                    1
12                21
123            321
1234        4321
12345    54321
123456654321.

Code:

import java.util.Scanner;
public class Pattern12
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int i,j,k;
        for (i=1;i<=n;i++) {
            for (j=1;j<=n;j++) {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            for (j=n;j>=1;j--)
            {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-11.

Explanation :
If n=6 then output will be
          1
        21
      321
    4321
  54321
654321.

Code:

import java.util.Scanner;
public class Pattern11
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for (int i=1;i<=n;i++)
        {
            for (int j=n;j>=1;j--)
            {
                if(j<=i)
                        System.out.printf("%d",j);
                else
                        System.out.printf(" ");
            }
            System.out.printf("\n");
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-10.

Explanation :
If n=6 then output will be
1
21
321
4321
54321
654321.

Code:

import java.util.Scanner;
public class Pattern10
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = i; j >= 1 ; j--)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-9.

Explanation :
If n=6 then output will be
123456
12345
1234
123
12
1
12
123
1234
12345
123456.

Code:

import java.util.Scanner;
public class Pattern6
{
    public static void main( String arg[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ck=0,c=2;
        while(c>0)
        {
           if(ck==0)
           {
              for(int i = 1,r = n ; i <= n ; i++,r--)
              {
                 for(int j = 1; j <= r; j++)
                 {
                     System.out.print(j);
                 }
                     System.out.println();     
               }
               ck++;
            }
           else
           {
              for(int i = 2;i <= n; i++)
              {
                 for(int j = 1;j <= i ; j++)
                 {
                    System.out.print(j);         
                 }
                    System.out.println();
              }
           }
               c--;
         }
        sc.close();
    }
}

Program to print a Number Pattern Type-8.

Explanation :
If n=6 then output will be
1
12
123
1234
12345
123456
12345
1234
123
12
1.

Code:

import java.util.Scanner;
public class Pattern8
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ck=0,c=2;
        while(c>0)
        {
            if(ck == 0)
            {
              for(int i = 1;i <= n ; i++)
              {
                for(int j =1 ; j <= i  ; j++)
                {
                  System.out.print(j);
                }
                  System.out.println();
               }
               ck++;     
             }
            else
            {
               for(int i =1 , r = n-1 ; i <= n - 1 ; i++ ,r--)
               {
                  for(int j = 1 ; j <= r; j++)
                  {
                     System.out.print(j);
                  }
                     System.out.println();
                }
             }
               c--;
      }
        sc.close();
    }
}

Program to print a Number Pattern Type-7.

Explanation :
If n=6 then output will be
123456
  23456
    3456
      456
        56
          6.

Code:

import java.util.Scanner;
public class Pattern7
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
         int n=sc.nextInt();
        for(int i = 1 ; i <= n ; i++)
        {
          for(int k = n - i ;  k < n - 1 ; k++)
          {
            System.out.print(" ");
          }
          for(int j = i ; j <= n ; j++)
          {
            System.out.print(j);
          }
           System.out.println();
        }
        sc.close();
     }
}

Program to print a Number Pattern Type-6.

Explanation :
If n=6 then output will be
123456
12345
1234
123
12
1.

Code:

import java.util.Scanner;
public class Pattern6
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i =1, r = n; i <= n ; i++ ,r--)
        {
            for(int j = 1 ; j <= r ; j++)
            {
                System.out.print(j);
            }
            System.out.println();      
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-5.

Explanation :
If n=6 then output will be
1
12
123
1234
12345
123456.

Code:

import java.util.Scanner;
public class Pattern5
{
    public static void main( String args[])
    {
        System.out.print("Enter the number of rows:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i = 1; i <= n ; i++)
        {
            for(int j = 1; j <= i ; j++)
            {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

Program to print a Number Pattern Type-4.

Explanation :
If n=6 then output will be
666666
566666
456666
345666
234566
123456.

Code:

import java.util.Scanner;
public class Pattern4
{
   
        public static void main( String args[])
        {
            System.out.print("Enter the number of rows:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int i,j,k;
            for (i=n;i>=1;i--)
            {
                k = i;
                for (j=1;j<=n;j++)
                {
                    if(k <= n)
                    {
                        System.out.printf("%d",k);
                    } else
                    {
                        System.out.print(n);
                    }
                    k++;
                }
                System.out.printf("\n");
            }
            sc.close();
        }
}

Program to print a Number Pattern Type-3.

Explanation :
If n=4 then output will be
1111
1   1
1   1
1111.

Code:

import java.util.Scanner;
public class Pattern3
{
   
        public static void main( String args[])
        {
            System.out.print("Enter the number of rows:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int i,j;
            for (i=1;i<=n;i++)
            {
                for (j=1;j<=n;j++)
                {
                    if(j==n || j==1 || i==1 || i==n)
                            System.out.printf("1");
                    else
                            System.out.printf(" ");
                }
                System.out.printf("\n");
            }
            sc.close();
        }
}

Program to print a Number Pattern Type-2.

Explanation :
If n=6 then output will be
 111111
100001
100001
100001
100001
 111111.

Code:

import java.util.Scanner;
public class Pattern2
{
        public static void main( String args[])
        {
            System.out.print("Enter the number of rows:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int i,j;
            for (i=1;i<=n;i++)
            {
                for (j=1;j<=n;j++)
                {
                    if(j==n || j==1 || i==1 || i==n)
                        System.out.printf("1");
                    else
                        System.out.printf("0");
                }
                System.out.printf("\n");
            }
            sc.close();
        }
}

Sunday, 19 June 2016

Program to print a Number Pattern Type-1.

Explanation :
If n=6 then output will be
66666666666
65555555556
65444444456
65433333456
65432223456
65432123456
65432223456
65433333456
65444444456
65555555556
66666666666.

Code:

import java.util.Scanner;
public class Pattern11
{
    public static void main(String args[])
    {
        int i,j;
        System.out.print("Enter any number:");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for (i=n; i >=1 ; i--)
        {
           for (j=n ; j >= i ; j--)
                System.out.print(j);
           for (j=1 ; j<(i*2)-1 ;j++)
                System.out.print(i);
           for (j=i+1;j<=n;j++)
                System.out.print(j);
           System.out.print("\n");
        }
      
        for (i=2;i<=n;i++)
        {
            for (j=n; j>=i; j--)
               System.out.print(j);
            for (j =1 ; j<(i*2)-1 ; j++)
               System.out.print(i);
            for (j=i+1 ;j<=n ;j++)
               System.out.print(j);
            System.out.print("\n");
        }
        sc.close();
}
}

Program to perform Multiplication of 2 Matrices.

Code:

import java.util.Scanner;
public class MatrixMultiplication
{
   public static void main(String args[])
   {
      int m, n, p, q, sum = 0, c, d, k;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();

      int first[][] = new int[m][n];

      System.out.println("Enter the elements of first matrix");

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the number of rows and columns of second matrix");
      p = in.nextInt();
      q = in.nextInt();

      if ( n != p )
         System.out.println("Matrices with entered orders can't be multiplied with each other.");
      else
      {
         int second[][] = new int[p][q];
         int multiply[][] = new int[m][q];

         System.out.println("Enter the elements of second matrix");

         for ( c = 0 ; c < p ; c++ )
            for ( d = 0 ; d < q ; d++ )
               second[c][d] = in.nextInt();

         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
            {  
               for ( k = 0 ; k < p ; k++ )
               {
                  sum = sum + first[c][k]*second[k][d];
               }

               multiply[c][d] = sum;
               sum = 0;
            }
         }

         System.out.println("Product of entered matrices:-");

         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
               System.out.print(multiply[c][d]+"\t");

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

Program to find the Transpose of a Matrix.

Code:

import java.util.Scanner;
public class Transpose
   public static void main(String args[])
   { 
          int m, n, c, d; 
          Scanner in = new Scanner(System.in); 
          System.out.println("Enter the number of rows and columns of matrix"); 
          m = in.nextInt(); n = in.nextInt(); int matrix[][] = new int[m][n]; 
         System.out.println("Enter the elements of matrix"); 
         for ( c = 0 ; c < m ; c++ ) 
             for ( d = 0 ; d < n ; d++ ) 
                matrix[c][d] = in.nextInt(); 
         int transpose[][] = new int[n][m]; 
         for ( c = 0 ; c < m ; c++ ) 
        {
            for ( d = 0 ; d < n ; d++ ) 
                transpose[d][c] = matrix[c][d]; 
         }
         System.out.println("Transpose of entered matrix:-"); 
        for ( c = 0 ; c < n ; c++ ) 
       { 
           for ( d = 0 ; d < m ; d++ ) 
               System.out.print(transpose[c][d]+"\t"); 
               System.out.print("\n");
        }
   } 
}

Program to Add or Subtract two Matrices.

Code:

import java.util.Scanner;
public class Add
{
     public static void main(String args[])
    { 
          int m, n, c, d; 
          Scanner in = new Scanner(System.in); 
          System.out.println("Enter the number of rows and columns of matrix"); 
          m = in.nextInt(); 
          n = in.nextInt(); 
         int first[][] = new int[m][n]; int second[][] = new int[m][n]; int sum[][] = new int[m][n]; 
         System.out.println("Enter the elements of first matrix"); 
         for ( c = 0 ; c < m ; c++ ) 
           for ( d = 0 ; d < n ; d++ ) 
              first[c][d] = in.nextInt(); 
        System.out.println("Enter the elements of second matrix"); 
        for ( c = 0 ; c < m ; c++ ) 
           for ( d = 0 ; d < n ; d++ ) 
              second[c][d] = in.nextInt(); 
       for ( c = 0 ; c < m ; c++ ) 
            for ( d = 0 ; d < n ; d++ ) 
               sum[c][d] = first[c][d] + second[c][d]; //replace '+' with '-' to subtract matrices     
      System.out.println("Sum of entered matrices:-"); 
      for ( c = 0 ; c < m ; c++ ) 
     {
          for ( d = 0 ; d < n ; d++ ) 
               System.out.print(sum[c][d]+"\t"); 
               System.out.println();
      }
  }
 }

Saturday, 18 June 2016

Program to prove that String Object is immutable or an example of String Pooling.

Code:

public class StringPooling
{
    public static void main( String args[])
    {
        String s1="a";
        String s2="a";
        System.out.println(s1==s2);//true
        System.out.println(s1.equals(s2));//true
       
        String s3=new String("a");
        String s4=new String("a");
        System.out.println(s3==s4);//false
        System.out.println(s3.equals(s4));//true
       
        String s5="a";
        String s6=new String("a");
        System.out.println(s5==s6);//false
        System.out.println(s5.equals(s6));//true
    }
}

Program to print the Words of a Given String ending with a Particular Letter.

Explanation :
If String= "All cars are supercars until they have tires".
1)If Letter = "s" then output will be "cars,supercars,tires".
2)If Letter = "l" then output will be "All,until".
3)If letter = "v" then output will be "There are no words ending with Letter v".

Code:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class EndsWithLetter
{   
     public static void main(String args[])
     {
        int count=0;
        ArrayList<String> ar= new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any String");
        String x=sc.nextLine();
        System.out.println("Enter a letter");
        String y=sc.nextLine();
        StringTokenizer st=new StringTokenizer(x,",/-@ ");
        while(st.hasMoreTokens())
        {
            String st1 = (String) st.nextToken();
            if(st1.endsWith(y))
            {
                ar.add(st1);//Adding Words in ArrayList
                count+=1;
            }
        }
        if(count!=0)
        {
                System.out.println("Words ending with letter "+y+" are:");
                for(String z:ar)
                {
                    System.out.println(z);
                }
        }
        else System.out.println("There are no words ending with Letter "+y);
        sc.close();
    }
}

Program to find the frequency of a SubString in a given String.

Explanation :
If String ="For each rose,a rose is a rose" and SubString ="rose" then,
output will be "rose has occured 3 times".

Code:

import java.util.Scanner;
import java.util.StringTokenizer;
public class SubStringCount
{
      public static void main(String args[])
     {
         int count=0;
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter any String");
         String x=sc.nextLine();
         System.out.println("Enter Sub String");
         String y=sc.nextLine();
         StringTokenizer st=new StringTokenizer(x,", ");
        while(st.hasMoreTokens())
       {
             String st1 = (String) st.nextToken();
               if(st1.equals(y))
              {
                   count ++;
               }
        }
             System.out.println(y+" has occured "+ count+ " times in "+x);
             sc.close();
}
}

Program to perform Selection Sort.

Code:

public class MySelectionSort
{
    public static int[] doSelectionSort(int[] arr)
    {    
        for (int i = 0; i < arr.length - 1; i++)
        {
            int index = i;
            for (int j = i + 1; j < arr.length; j++)
                if (arr[j] < arr[index])
                    index = j;
     
            int smallerNumber = arr[index]; 
            arr[index] = arr[i];
            arr[i] = smallerNumber;
        }
        return arr;
    }
    
    public static void main(String a[])
    {        
        int[] arr1 = {10,34,2,56,7,67,88,42};
        int[] arr2 = doSelectionSort(arr1);
        for(int i:arr2)
        {
            System.out.println(i);
           
        }
    }
}

Program to perform Insertion Sort.

Code:

public class MyInsertionSort
{
    public static void main(String a[])
    {
        int[] arr1 = {10,34,2,56,7,67,88,42};
        int[] arr2 = doInsertionSort(arr1);
        for(int i:arr2)
        {
            System.out.print(i);
            System.out.print(", ");
        }
    }
    
    public static int[] doInsertionSort(int[] input)
    {    
        int temp;
        for (int i = 1; i < input.length; i++)
        {
            for(int j = i ; j > 0 ; j--)
            {
                if(input[j] < input[j-1])
                {
                    temp = input[j];
                    input[j] = input[j-1];
                    input[j-1] = temp;
                }
            }
        }
        return input;
    }
}