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();
}
}
}
No comments:
Post a Comment