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 6 of 50+ number patterns, you can find all number patterns here.
Visit the following links to check mixed pattern programs
Click on the pattern given below and you will get redirected to the code.
Pattern 1
12345 1234 123 12 5 45 345 2345 12345
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 < 5; j++) {
if((j+i>=8))
System.out.print(j+1);
else if(j-i>=0)
System.out.print((j-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 < 5; j++) {
if ((j + i >= 8)) {
std::cout << j + 1;
} else if (j - i >= 0) {
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 < 9) :
j = 0
while (j < 5) :
if ((j + i >= 8)) :
print(j + 1, end ="")
elif(j - i >= 0) :
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 < 9; i++) {
for (j; j < 5; j++) {
if ((j + i >= 8)) {
console.log(j + 1);
} else if (j - i >= 0) {
console.log((j - i) + 1);
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
1 121 12321 1234321 123454321
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=i+2) {
int num=1;
for (int j = 0; j < 9; j++) {
if(j-i<=0)
System.out.print(num);
if(2*j-i<0)
num++;
else
num--;
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 9; i = i + 2) {
int num = 1;
for (int j = 0; j < 9; j++) {
if (j - i <= 0) {
std::cout << num;
}
if (2 * j - i < 0) {
num++;
} else {
num--;
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 9) :
num = 1
j = 0
while (j < 9) :
if (j - i <= 0) :
print(num, end ="")
if (2 * j - i < 0) :
num += 1
else :
num -= 1
j += 1
print()
i = i + 2
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 9; i = i + 2) {
var num = 1;
for (j; j < 9; j++) {
if (j - i <= 0) {
console.log(num);
}
if (2 * j - i < 0) {
num++;
} else {
num--;
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
1 121 12321 1234321 123454321
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++) {
int num=1;
for (int j = 0; j < 9; j++) {
if(j+i>=4 && j-i<=4){
System.out.print(num);
if(j<4)
num++;
else
num--;
}
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#include
int main() {
for (int i = 0; i < 5; i++) {
int num = 1;
for (int j = 0; j < 9; j++) {
if (j + i >= 4 && j - i <= 4) {
std::cout << num;
if (j < 4) {
num++;
} else {
num--;
}
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 5) :
num = 1
j = 0
while (j < 9) :
if (j + i >= 4 and j - i <= 4) :
print(num, end ="")
if (j < 4) :
num += 1
else :
num -= 1
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 5; i++) {
var num = 1;
for (j; j < 9; j++) {
if (j + i >= 4 && j - i <= 4) {
console.log(num);
if (j < 4) {
num++;
} else {
num--;
}
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 4
1 2 6 9 3 7 10 12 13
Code for pattern 4
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
int num=1;
for (int i = 0; i < 5; i=i+2) {
int num2=num;
int diff=4;
for (int j = 0; j < 5; j++) {
if(j-i<=0)
{
System.out.print(num2+" ");
num2=num2+diff;
diff--;
}
}
System.out.println();
num++;
}
}
}
C++
#include
int main() {
int num = 1;
for (int i = 0; i < 5; i = i + 2) {
int num2 = num;
int diff = 4;
for (int j = 0; j < 5; j++) {
if (j - i <= 0) {
std::cout << std::to_string(num2) + " ";
num2 = num2 + diff;
diff--;
}
}
std::cout << std::endl;
num++;
}
return 0;
};
Python
class MainClass :
@staticmethod
def main( args) :
num = 1
i = 0
while (i < 5) :
num2 = num
diff = 4
j = 0
while (j < 5) :
if (j - i <= 0) :
print(str(num2) + " ", end ="")
num2 = num2 + diff
diff -= 1
j += 1
print()
num += 1
i = i + 2
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
var num = 1;
for (i; i < 5; i = i + 2) {
var num2 = num;
var diff = 4;
for (j; j < 5; j++) {
if (j - i <= 0) {
console.log(num2 + " ");
num2 = num2 + diff;
diff--;
}
}
console.log();
num++;
}
}
}
MainClass.main([]);