Pattern Problem | Rectangle Pattern


Test Case #1

Enter length:
10
Enter breadth:
4

Output

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

Test Case #2

Enter length:
50
Enter breadth:
10

Output

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

Program

import java.util.*;

package jothibasu.com;

public class RectanglePattern {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int length,breadth;
System.out.println("Enter length:");
length = scan.nextInt();
System.out.println("Enter breadth:");
breadth = scan.nextInt();
//First Line
for (int i = 0 ; i < length ; i++)
System.out.print("*");
//Moving to next line
System.out.println();
/*Execute the loop up to breadth
  Here [breadth-2] given because
  of considering 1st and last line*/
for (int j = 0 ; j < breadth - 2 ; j++) {
//First * of breadth
System.out.print("*");
//Printing the space inside rectangle
for (int k = 0 ; k < length - 2 ; k++)
System.out.print(" ");
//Last * of breadth
System.out.print("*");
//Moving to next line
System.out.println();
}
//Last line
for (int l = 0 ; l < length ; l++)
System.out.print("*");
//Closing the scanner object
scan.close();
}
}

Pattern Problem | Alphabets

Test Case #1

Enter limit:
Z
Enter difference:
1

Output 

A
C
E
G
I
K
M
O
Q
S
U
W
Y

Test Case #2

Enter limit:
Z
Enter difference:
2

Output

AD
GJ
MP
SV
Y

Test Case #3

Enter limit:
Z
Enter difference:
3

Output

AEI
MQU
Y

Program


import java.util.*;

package jothibasu.com;

public class Pattern {

   public static void main(String[] args) {
int counter = 1;
char limit;
int difference;
Scanner scan = new Scanner(System.in);
System.out.println("Enter limit:");
limit = scan.next().charAt(0);
System.out.println("Enter difference:");
difference = scan.nextInt();
//Loop will execute up to the given limit
for (char value = 'A'; value <= limit ; value++) {
System.out.print(value);
//Converting character into integer 
                  [Type Conversion, ASCII value]
int charToInteger = (int) value;
//Add the difference to that integer value
charToInteger = charToInteger + difference;
//Converting integer to character
value = (char) charToInteger;
//Move to new line based on the difference given
if (counter % difference == 0)
System.out.println("");
counter++;
//Closing the scanner class object
scan.close();
}
}
}