Pin It

Pattern problems : Write a Java program to print same character on each line and having character printed in diagonal form.

Program to Pattern problems : Write a Java program to print same character on each line and having character printed in diagonal form. As shown below :

*
   *
      *
         *
            *
               *



package com.hubberspot.patterns.problems;

import java.util.Scanner;

public class DiagonalPattern {

    public static void main(String[] args) {

        String pattern;
        int noOfTimes;

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the pattern to print : ");
        pattern = scanner.nextLine();

        System.out.print("Enter number of times it should get printed : ");
        noOfTimes = scanner.nextInt();

        for(int i = 1; i <= noOfTimes; i++) {

            for(int j = 1; j <= i; j++) { 

                if(j != i) {

                    System.out.print(" ");

                }
                else if(j == i){

                    System.out.print(pattern);    

                }
            }
            System.out.println();
        }


    }

}


Output of the program : 

 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot