Â
Hello Readers,
In this post we are going to see, how to print "DIAMOND" "PATTERN" OF "PALINDROME" NUMBERS in C++, JAVA, PYTHON which is as follows:-
Logic to print the pattern of Diamond Pattern of "PALINDROME" numbers:-
1. Assign six variables as a,num,space,i,j,k. And declare the value of 'a'=1.
2. Use variable 'num' to take input of number of rows from the user.
3. Declare variable 'space' as 'n-1'.
4. Use While loop, and the condition is that the loop will be executed till the space value is not equal to zero i.e. while(space != 0)
5. Inside this loop print the gap between columns i.e. " ". Ans use post incrementation for space. Â
6. print the value of a and then the code for the next line i.e."\n".
7.Now change the value of space to 'num - 2'.
8. Now Use For Loop and use variable 'i' as iterator, its value will start from 1 till the input value i.e.(num),use post incrementation for 'i'.
9. Inside this loop re declare the value of 'a' as 1
10.Using Nested For Loop(loop inside another loop) take another iterator as 'j', start its value from 1 and it will last till the value of 'space', use post incrementation for 'j'.
11. Inside this loop print the gap between two columns i.e. (" ").
12.Outside this loop take another loop with iterator 'k', start its value from 0, till (i*2), use post incrementation for k.
13.Inside this loop use conditional statement, if value of 'j' is greater than 'i' then print the valueof 'a',and increment the value of 'a'. or else print 'a' but decrement the value of 'a'.
14.Close the conditional statement as well as for loop.
15.Outside this loop print the code for the next line i.e.("\n") and decrement the value of 'space' i.e. (space--)
16.Take another loop with iterator 'i', start its value from (num-1) till its greater than or equal to 1, use post decrementation for 'i'
17. Inside this loop Re-declare the value of 'space' as (num - 1) and 'a' as 1.
18. Inside this loop, take another loop with iterator 'k' , assign the value of 'k' as '1', it value will last till the value of 'space' , use post incrementation for 'k'.
19. inside this loop, print the gap between two columns i.e. (" ").
Â
Â
20. Outside this loop take another loop with iterator 'j', start its value from 1, till (i*2)-1, use post incrementation for k.
21. Inside this loop use conditional statement, if value of 'j' is less than 'i' then print the value of 'a',and increment the value of 'a'. or else print 'a' but decrement the value of 'a'.
22. Close the conditional statement as well as for loop.
23. Outside this loop print the code for the next line i.e.("\n") and decrement the value of 'space' i.e. (space--)
24. In this way you can print the "Diamond pattern of Palindrome Numbers".
DIAMOND PATTERN OF PALINDROME NUMBERS:
      1
     121
    12321
     121
     1
C++
//**HELPFORCODERS**
//This is a Pattern project which prints
// 1
// 121
// 12321
//1234321
// 12321
// 121
// 1
#include <iostream>
using namespace std;
int main()
{
int a = 1, num, space;
cout << "Enter the number of Rows \n";
cin >> num;
space = num - 1;
while (space != 0)
{
cout << " ";
space--;
}
cout << a << "\n";
space = num - 2;
for (int i = 1; i < num; i++)
{
a = 1;
for (int k = 1; k <= space; k++)
{
cout << " ";
}
for (int j = 0; j <= i * 2; j++)
{
if (j < i)
{
cout << a;
a++;
}
else
{
cout << a;
a--;
}
}
cout << "\n";
space--;
}
for (int i = num - 1; i >= 1; i--)
{
space = num - i;
a = 1;
for (int k = 1; k <= space; k++)
{
cout << " ";
}
for (int j = 1; j <= i * 2 - 1; j++)
{
if (j < i)
{
cout << a;
a++;
}
else
{
cout << a;
a--;
}
}
cout << "\n";
space--;
}
return 0;
}
  Â
Output For C++
Java
//**HELPFORCODERS**
//This is a Pattern project which prints
// 1
// 121
// 12321
//1234321
// 12321
// 121
// 1
package com.company;
import java.util.*;
public class Pattern
{
public static void main(String[] args)
{
int rows, i, space, star = 1;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of Rows ");
rows = scanner.nextInt();
for (i = 1; i <= rows; i++)
{
for (space = 1; space <= rows - i; space++)
{
System.out.print(" ");
}
while (star <= (2 * i - 1))
{
System.out.print(-Math.abs(star - i) + i);
star++;
}
star = 1;
System.out.print("\n");
}
for (i = rows-1; i >= 1; i--)
{
for (space = rows - i; space >= 1; space--)
{
System.out.print(" ");
}
while (star <= (2 * i - 1))
{
System.out.print(-Math.abs(star - i) + i);
star++;
}
star = 1;
System.out.print("\n");
}
}
}
Output for JAVA
Python
#**HELPFORCODERS**
#This is a Pattern project which prints
# 1
# 121
# 12321
#1234321
# 12321
# 121
# 1
n = int(input("Enter the number of rows"))
# putting the space in line 1
for i in range(1, n * 2 + 1):
print(end=" ")
print("1")
for i in range(1, n * 2):
# printing the increasing pattern
if (i < n):
for j in range(1, (n - i) * 2 + 1):
print(end=" ")
else:
for j in range(1, (i % n) * 2 + 1):
print(end=" ")
if (i < n):
for j in range(i % n + 1):
print(j+1, end=" ")
for j in range(i % n - 1, -1, -1):
print(j+1, end=" ")
# printing the decreasing pattern
elif (i > n):
for j in range(n - (i - n) + 1):
print(j+1, end=" ")
for j in range((n - (i - n)) - 1, -1, -1):
print(j+1, end=" ")
else:
for j in range(n):
print(j+1, end=" ")
for j in range(n, -1, -1):
print(j+1, end=" ")
print()
# putting the space in last line
for i in range(0, n * 2 ):
print(end=" ")
print("1", end="")
Output for PYTHON
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP