In this article you will learn how to print different types of number patterns. This is a set 5 of the number pattern series, you can find all number patterns here.
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.
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 2 1 2 3 1 2 3 4 1 2 3 4 5
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>=4)
System.out.print((j+i)-3+" ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j + i >= 4) {
std::cout << std::to_string((j + i) - 3) + " ";
} 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 < 5) :
if (j + i >= 4) :
print(str((j + i) - 3) + " ", 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 < 5; j++) {
if (j + i >= 4) {
console.log((j + i) - 3 + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Code for pattern 2
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
int count =1;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if(j-i<=0 )
System.out.print(count+++ " ");//
else
System.out.print("");
}
System.out.println();
}
}
}
C++
#include
int main() {
int count = 1;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j - i <= 0) {
std::cout << std::to_string(count++) + " ";
} else {
std::cout << "";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
count = 0
i = 0
while (i < 5) :
j = 0
while (j < 5) :
if (j - i <= 0) :
count = count+1
print(str(count) + " ", end ="")
else :
print("", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
var count = 1;
for (i; i < 5; i++) {
for (j; j < 5; j++) {
if (j - i <= 0) {
console.log(count++ + " ");
} else {
console.log("");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
12345 1234 123 12 1 12 123 1234 12345
Code for pattern 3
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
int count =1;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
if(j-i<=-4 || j+i<=4)
System.out.print(j+1);//
else
System.out.print("");
}
System.out.println();
}
}
}
C++
#include
int main() {
int count = 1;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 5; j++) {
if (j - i <= -4 || j + i <= 4) {
std::cout << j + 1;
} else {
std::cout << "";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
count = 1
i = 0
while (i < 9) :
j = 0
while (j < 5) :
if (j - i <= -4 or j + i <= 4) :
print(j + 1, end ="")
else :
print("", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
var count = 1;
for (i; i < 9; i++) {
for (j; j < 5; j++) {
if (j - i <= -4 || j + i <= 4) {
console.log(j + 1);
} else {
console.log("");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 4
1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 4 5 3 4 5 2 3 4 5 1 2 3 4 5
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>=8))
System.out.print((j+1)+" ");
else if(j-i>=0)
System.out.print((j+1)+" ");
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)) {
std::cout << std::to_string((j + 1)) + " ";
} else if (j - i >= 0) {
std::cout << std::to_string((j + 1)) + " ";
} 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)) :
print(str((j + 1)) + " ", end ="")
elif(j - i >= 0) :
print(str((j + 1)) + " ", 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)) {
console.log((j + 1) + " ");
} else if (j - i >= 0) {
console.log((j + 1) + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);