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 3 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
000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999
Code for pattern 1
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 30; j++) {
System.out.print(j/3);
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 30; j++) {
std::cout << j / 3;
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 8) :
j = 0
while (j < 30) :
print(int(j / 3), end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 8; i++) {
for (j; j < 30; j++) {
console.log(parseInt(j / 3));
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Code for pattern 2
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(i<=4 && j+i>=4)
System.out.print(i+1+" ");//
else if(i>=5 && j-i>=-4)
System.out.print(9-i+" ");
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 (i <= 4 && j + i >= 4)
{
std::cout << std::to_string(i + 1) + " ";
}
else if (i >= 5 && j - i >= -4)
{
std::cout << std::to_string(9 - i) + " ";
}
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 (i <= 4 and j + i >= 4) :
print(str(i + 1) + " ", end ="")
elif(i >= 5 and j - i >= -4) :
print(str(9 - i) + " ", 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 (i <= 4 && j + i >= 4) {
console.log(i + 1 + " ");
} else if (i >= 5 && j - i >= -4) {
console.log(9 - i + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9
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 < 9; j++) {
if(j+i>=8)
System.out.print(i+1+" ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (j + i >= 8) {
std::cout << std::to_string(i + 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 < 9) :
if (j + i >= 8) :
print(str(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 < 9; i++) {
for (j; j < 9; j++) {
if (j + i >= 8) {
console.log(i + 1 + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);