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

No comments:

Post a Comment

No of Occurrence in Array

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