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 1 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 12 123 1234 12345
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(j+1);//in vertical line take j+1 equation
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 <= 0) {
std::cout << j + 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 < 5) :
if (j - i <= 0) :
print(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 < 5; i++) {
for (j; j < 5; j++) {
if (j - i <= 0) {
console.log(j + 1);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
5 54 543 5432 54321
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(5-j);//in vertical line take j+1 equation
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 <= 0) {
std::cout << 5 - 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 < 5) :
if (j - i <= 0) :
print(5 - 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 < 5; j++) {
if (j - i <= 0) {
console.log(5 - j);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
12345 2345 345 45 5
Code for pattern 3
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+1);
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 << j + 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 < 5) :
if (j + i <= 4) :
print(j + 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 < 5; j++) {
if (j + i <= 4) {
console.log(j + i + 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 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 < 10; i++) {
for (int j = 0; j < 5; j++) {
if(j-i>=0 || j+i>=9)
System.out.print(j+1+" ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
if (j - i >= 0 || j + i >= 9) {
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 < 10) :
j = 0
while (j < 5) :
if (j - i >= 0 or j + i >= 9) :
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 < 10; i++) {
for (j; j < 5; j++) {
if (j - i >= 0 || j + i >= 9) {
console.log(j + 1 + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 5
5 4 5 3 4 5 2 3 4 5 1 2 3 4 5
Code for pattern 5
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+1+" ");
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 + 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 < 5) :
if (j + i >= 4) :
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 < 5; i++) {
for (j; j < 5; j++) {
if (j + i >= 4) {
console.log(j + 1 + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);