Considering that you have been following our series in which you learn how to build star patterns , English alphabet patterns and character patterns. Now time to print some number patterns.
As per my personal opinion number patterns are more important and asked more frequently in the coding interview. So to be ready for coding round I suggest you to learn printing number patterns.
In this blog we have shared set 7 of 50+ number patterns, you can find all number patterns here.
Here are the some quick links for more patterns
Click on the pattern given below and you will get redirected to the code.
Pattern 1
1 1 12 21 123 321 1234 4321 1234554321
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 < 10; j++) {
if(j-i<=0)
System.out.print(j+1);
else if(j+i>=9)
System.out.print(10-j);
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
if (j - i <= 0) {
std::cout << j + 1;
} else if (j + i >= 9) {
std::cout << 10 - j;
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 5) :
j = 0
while (j < 10) :
if (j - i <= 0) :
print(j + 1, end ="")
elif(j + i >= 9) :
print(10 - j, end ="")
else :
print(" ", 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 < 10; j++) {
if (j - i <= 0) {
console.log(j + 1);
} else if (j + i >= 9) {
console.log(10 - j);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
00*00 00*00 ***** 00*00 00*00
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==2 || i==2)
System.out.print("*");//
else
System.out.print("0");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 2 || i == 2) {
std::cout << "*";
} else {
std::cout << "0";
}
}
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 == 2 or i == 2) :
print("*", end ="")
else :
print("0", 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 == 2 || i == 2) {
console.log("*");
} else {
console.log("0");
}
}
console.log();
}
}
}
MainClass.main([]);
Code for pattern 3
1 1 22 22 333 333 4444 4444 5555555555
Code
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 < 10; j++) {
if(j-i<=0)
System.out.print(i+1);//
else if(j+i>=9)
System.out.print(i+1);//
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
if (j - i <= 0) {
std::cout << i + 1;
} else if (j + i >= 9) {
std::cout << i + 1;
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 5) :
j = 0
while (j < 10) :
if (j - i <= 0) :
print(i + 1, end ="")
elif(j + i >= 9) :
print(i + 1, end ="")
else :
print(" ", 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 < 10; j++) {
if (j - i <= 0) {
console.log(i + 1);
} else if (j + i >= 9) {
console.log(i + 1);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);