In coding interview interviewer may ask you print any number pattern , alphabet pattern or an character patterns. We recommend you to follow our patterns series so you will feel comfortable with printing patterns. In this blog post you will find set 2 of character patterns.
We recommend you to solve other types patterns as well –
Click on the pattern and you will get redirected to the code.
Pattern 1
FEDCBA EDCBA DCBA CBA BA A A BA CBA DCBA EDCBA FEDCBA
Code for pattern 1
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 6; j++) {
if(j-i<=-6)
System.out.print((char)(i-j-6+65));
else if(j+i<=5)
System.out.print((char)(5-(j+i)+65));
else
System.out.print("");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 6; j++) {
if (j - i <= -6) {
std::cout << (char)(i - j - 6 + 65);
} else if (j + i <= 5) {
std::cout << (char)(5 - (j + i) + 65);
} else {
std::cout << "";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 12) :
j = 0
while (j < 6) :
if (j - i <= -6) :
print(chr((i - j - 6 + 65)), end ="")
elif(j + i <= 5) :
print(chr((5 - (j + i) + 65)), end ="")
else :
print("", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 12; i++) {
for (j; j < 6; j++) {
if (j - i <= -6) {
console.log(String.fromCharCode((i - j - 6 + 65)));
} else if (j + i <= 5) {
console.log(String.fromCharCode((5 - (j + i) + 65)));
} else {
console.log("");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
A A B A B C A B C D A B C D E A B C D E F
Code for pattern 2
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if(j+i>=5)
System.out.print((char)((j+i)-5+65)+" ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (j + i >= 5) {
std::cout << std::to_string((char)((j + i) - 5 + 65)) + " ";
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 6) :
j = 0
while (j < 6) :
if (j + i >= 5) :
print(str(chr(((j + i) - 5 + 65))) + " ", end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 6; i++) {
for (j; j < 6; j++) {
if (j + i >= 5) {
console.log(String.fromCharCode(((j + i) - 5 + 65)) + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
FEDCBA EDCBA DCBA CBA BA A
Code for pattern 3
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
char a ='F';
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if(i+j<6)
System.out.print((char)(a-j-i ));
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
char a = 'F';
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (i + j < 6) {
std::cout << (char)(a - j - i);
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
a = 'F'
i = 0
while (i < 6) :
j = 0
while (j < 6) :
if (i + j < 6) :
print(chr((ord(a) - j - i)), end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
var a = 'F';
for (i; i < 6; i++) {
for (j; j < 6; j++) {
if (i + j < 6) {
console.log(String.fromCharCode((a.charCodeAt(0) - j - i)));
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 4
F FE FED FEDC FEDCB FEDCBA
Code for pattern 4
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if(j-i<=0)
System.out.print((char)(5-j +65));
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (j - i <= 0) {
std::cout << (char)(5 - j + 65);
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 6) :
j = 0
while (j < 6) :
if (j - i <= 0) :
print(chr((5 - j + 65)), end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass
{
static main(args)
{
for (i; i < 6; i++)
{
for (j; j < 6; j++)
{
if (j - i <= 0)
{
console.log(String.fromCharCode((5 - j + 65)));
}
else
{
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 5
ABCDEF ABCDE ABCD ABC AB A
Code for pattern 5
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if(j+i<=5)
System.out.print((char)(j+65));
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (j + i <= 5) {
std::cout << (char)(j + 65);
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 6) :
j = 0
while (j < 6) :
if (j + i <= 5) :
print(chr((j + 65)), end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 6; i++) {
for (j; j < 6; j++) {
if (j + i <= 5) {
console.log(String.fromCharCode((j + 65)));
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);