When we talk about the interview questions apart from theoretical part coding skill is also important. If you have been following our patterns series then you are on the right track of cracking the coding interview.
In this series we have learn how to print star patterns , English alphabet patterns and character patterns. Now time to print some number patterns.
Below you will find the code to print different types of number pattern using Java , C++ , Python and JavaScript.
In this blog we have shared set 4 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 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9
Code for pattern 1
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((j+i)-7+" ");
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((j + i) - 7) + " ";
} 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((j + i) - 7) + " ", 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((j + i) - 7 + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 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 < 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 << 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 < 9) :
if (j - i >= 0) :
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 < 9; j++) {
if (j - i >= 0) {
console.log(9 - i + " ");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
1 12 123 1234 12345 1234 123 12 1
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 < 5; j++) {
if(j-i<=0 && j+i<=8)
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 <= 0 && j + i <= 8) {
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 < 5) :
if (j - i <= 0 and j + i <= 8) :
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 < 5; j++) {
if (j - i <= 0 && j + i <= 8) {
console.log(j + 1);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);