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 2 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 2 2 3 3 3 4 4 4 4 5 5 5 5 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(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 << 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 < 5) :
j = 0
while (j < 5) :
if (j + i >= 4) :
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 < 5; i++) {
for (j; j < 5; j++) {
if (j + i >= 4) {
console.log(i + 1 + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
1 12 123 1234 12345 123456 1234567 12345678 123456789
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 < 9; j++) {
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 < 9; 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 < 9) :
j = 0
while (j < 9) :
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 < 9; i++) {
for (j; j < 9; j++) {
if (j - i <= 0) {
console.log(j + 1);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
1 22 333 4444 55555 666666 7777777 88888888 999999999
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<=0)
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 <= 0) {
std::cout << 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 <= 0) :
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 < 9; i++) {
for (j; j < 9; j++) {
if (j - i <= 0) {
console.log(i + 1);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 4
9 98 987 9876 98765 987654 9876543 98765432 987654321
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 < 9; j++) {
if(j-i<=0)
System.out.print(9-j);
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 <= 0) {
std::cout << 9 - j;
} 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 <= 0) :
print(9 - j, 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 <= 0) {
console.log(9 - j);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 5
9 88 777 6666 55555 444444 3333333 22222222 111111111
Code for pattern 5
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<=0)
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 < 9; j++) {
if (j - i <= 0) {
std::cout << 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 < 9) :
if (j - i <= 0) :
print(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 < 9; j++) {
if (j - i <= 0) {
console.log(9 - i);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);