Program to Pattern problems : Write a Java program to print same character on each line and having character printed in Inverted V Shape form. As shown below :
*
* *
* *
* *
* *
Output of the program :
*
* *
* *
* *
* *
package com.hubberspot.patterns.problems;
import java.util.Scanner;
public class InvertedVPattern {
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();
int spaceBeforePattern = noOfTimes-1;
int spaceAfterPattern = -1;
System.out.println();
for(int i=1; i <= noOfTimes; i++ ) {
for(int j = 1; j <= spaceBeforePattern; j++) {
System.out.print(" ");
}
System.out.print(pattern);
for(int k = 1; k <=spaceAfterPattern; k++) {
System.out.print(" ");
}
if(i > 1) {
System.out.print(pattern);
}
System.out.println();
spaceAfterPattern = spaceAfterPattern + 2;
spaceBeforePattern = spaceBeforePattern - 1;
}
}
}
Output of the program :
