We know that coding is not for everyone , but every coder must know how to print patterns. In technical round you will ask to write a program to print 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 1 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
A AB ABC ABCD ABCDE
Code for pattern 1
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if(j-i<=0)
System.out.print((char)(j+65));
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j - i <= 0) {
std::cout << (char)(j + 65);
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 5) :
j = 0
while (j < 5) :
if (j - i <= 0) :
print(chr((j + 65)), end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 5; i++) {
for (j; j < 5; j++) {
if (j - i <= 0) {
console.log(String.fromCharCode((j + 65)));
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
A BB CCC DDDD EEEEE
Code for pattern 2
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if(j-i<=0)
System.out.print((char) (i+65));
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j - i <= 0) {
std::cout << (char)(i + 65);
}
}
std::cout << std::endl;
}
return 0;
};
Python
class Pattern1 :
@staticmethod
def main( args) :
i = 0
while (i < 5) :
j = 0
while (j < 5) :
if (j - i <= 0) :
print(chr((i + 65)), end ="")
j += 1
print()
i += 1
if __name__=="__main__":
Pattern1.main([])
JavaScript
class Pattern1 {
static main(args) {
for (i; i < 5; i++) {
for (j; j < 5; j++) {
if (j - i <= 0) {
console.log(String.fromCharCode((i + 65)));
}
}
console.log();
}
}
}
Pattern1.main([]);
Pattern 3
A BB CCC DDDD EEEEE DDDD CCC BB A
Code for pattern 3
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
if(j+i<=8 && i>=4)
System.out.print((char)(8-i+65));
else if(j-i<=0 && i<=4)
System.out.print((char)(i+65));
else
System.out.print("");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
if (j + i <= 8 && i >= 4) {
std::cout << (char)(8 - i + 65);
} else if (j - i <= 0 && i <= 4) {
std::cout << (char)(i + 65);
} else {
std::cout << "";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 9) :
j = 0
while (j < 5) :
if (j + i <= 8 and i >= 4) :
print(chr((8 - i + 65)), end ="")
elif(j - i <= 0 and i <= 4) :
print(chr((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 < 9; i++) {
for (j; j < 5; j++) {
if (j + i <= 8 && i >= 4) {
console.log(String.fromCharCode((8 - i + 65)));
} else if (j - i <= 0 && i <= 4) {
console.log(String.fromCharCode((i + 65)));
} else {
console.log("");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 4
A AB ABC ABCD ABCDE ABCD ABC AB A
Code for pattern 4
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
if(j-i<=0 && j+i<=8 )
System.out.print((char)(j+65));
else
System.out.print("");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
if (j - i <= 0 && j + i <= 8) {
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 < 9) :
j = 0
while (j < 5) :
if (j - i <= 0 and j + i <= 8) :
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 < 9; i++) {
for (j; j < 5; j++) {
if (j - i <= 0 && j + i <= 8) {
console.log(String.fromCharCode((j + 65)));
} else {
console.log("");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 5
ABCDEF ABCDE ABCD ABC AB A A AB ABC ABCD ABCDE ABCDEF
Code for pattern 5
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 || 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 < 12; i++) {
for (int j = 0; j < 6; j++) {
if (j - i <= -6 || 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 < 12) :
j = 0
while (j < 6) :
if (j - i <= -6 or 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 < 12; i++) {
for (j; j < 6; j++) {
if (j - i <= -6 || j + i <= 5) {
console.log(String.fromCharCode((j + 65)));
} else {
console.log("");
}
}
console.log();
}
}
}
MainClass.main([]);