Saturday, 27 January 2018

Write a Java Program to draw number pattern.

Example : 1

package com.tutorial4u;

import java.util.Scanner;

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

}



Output : 

Enter number of rows : 6

1
12
123
1234
12345
123456


Example : 2


package com.tutorial4u;

import java.util.Scanner;

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

}



Output :

Enter number of rows : 6

1
21
321
4321
54321
654321

Example :3

package com.tutorial4u;

import java.util.Scanner;

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

}


Output :

Enter number of rows : 6

1
22
333
4444
55555
666666

Write a Java Program to print Star Pattern

Example : 1

package com.tutorial4u;

import java.util.Scanner;

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

}



Output : 

Enter the number of rows : 5

*
**
***
****
*****

Example : 2


package com.tutorial4u;

import java.util.Scanner;

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

}



Output : 

Enter the number of rows : 7

*******
******
*****
****
***
**
*


Example : 3


package com.tutorial4u;

import java.util.Scanner;

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

}


Output :

Enter the number of rows : 7

*******
 ******
  *****
   ****
    ***
     **
      *


Example : 4


package com.tutorial4u;

import java.util.Scanner;

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

}


Output : 

Enter the number of rows : 8

       *
      **
     ***
    ****
   *****
  ******
 *******

Example : 5


package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           {
                for(int i=1; i<=numi++)
                {
                for(int j=num-1; j>=ij--)
                {
                System.out.print(" ");
                }
                for(int k=1; k<=(2*i-1); k++)
                {
                System.out.print("*");
                }
                System.out.println("");
                }
           }
     }
}



Output : 

Enter the number of rows : 5

    *
   ***
  *****
 *******
*********


Example:6


package com.tutorial4u;

import java.util.Scanner;

public class Triangle {
     public static void main(String[] args) {
           int num;
           System.out.print("Enter the number of rows : ");
           Scanner sc = new Scanner(System.in);
           num = sc .nextInt();
           {
            for(int i=num;i>=1;i--)
            {
                 for(int j = num ; j>i;j--)
                 {
                       System.out.print(" ");
                 }
                for(int k=1;k<(i*2);k++)
                {
                System.out.print("*");
                }
                System.out.println();
                }
           }
     }
}



Output : 

Enter the number of rows : 7

*************
 ***********
  *********
   *******
    *****
     ***
      *

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...