Program to print the following pattern of a half diamond for N.
Example:
Input: N = 5
Output:1
2*3
4*5*6
7*8*9*10
11*12*13*14*15
11*12*13*14*15
7*8*9*10
4*5*6
2*3
1
This program is divided into four parts.
C++
// C++ program to print the half diamond // pattern using numbers and stars #include <iostream> using namespace std; void pattern( int N) { int i, j, count = 1; // This is upper half of pattern for (i = 1; i <= N; i++) { // for loop to display number // upto i for (j = 1; j <= i; j++) { if (j < i) cout << count++ << "*" ; else cout << count++; } cout << endl; } count = count - N; // This is lower half of pattern for (i = N; i >= 1; i--) { // for loop to display number // upto i for (j = 1; j <= i; j++) { if (j < i) cout << count++ << "*" ; else cout << count++; } count = (count + 1) - 2 * i; cout << endl; } } // Driver code int main() { int N = 4; // Function call pattern(N); return 0; } |
Java
// Java program to print the half diamond // pattern using numbers and stars public class GFG { // Function to print half diamond // pattern using numbers and stars public static void pattern( int N) { int i, j, count = 1 ; // This is upper half of pattern for (i = 1 ; i <= N; i++) { // for loop to display number // upto i for (j = 1 ; j <= i; j++) { if (j < i) System.out.print(count++ + "*" ); else System.out.print(count++); } System.out.println(); } count = count - N; // This is lower half of pattern for (i = N; i >= 1 ; i--) { // for loop to display number // upto i for (j = 1 ; j <= i; j++) { if (j < i) System.out.print(count++ + "*" ); else System.out.print(count++); } count = (count + 1 ) - 2 * i; System.out.println(); } } // Driver code public static void main(String[] args) { int N = 4 ; // Function call pattern(N); } } |
Python3
def pattern(N): count = 1 # This is upper half of pattern for i in range ( 1 , N + 1 ): for j in range ( 1 , i + 1 ): if j < i: print (count, end = '*' ) else : print (count, end = '') count + = 1 print () count = count - N # This is lower half of pattern for i in range (N, 0 , - 1 ): for j in range ( 1 , i + 1 ): if j < i: print (count, end = '*' ) else : print (count, end = '') count + = 1 count = (count + 1 ) - 2 * i print () # Driver code if __name__ = = '__main__' : N = 4 # Function call pattern(N) |
C#
// C# program to print the half diamond // pattern using numbers and stars using System; public class Program { static void pattern( int N) { int i, j, count = 1; // This is upper half of pattern for (i = 1; i <= N; i++) { // for loop to display number // upto i for (j = 1; j <= i; j++) { if (j < i) Console.Write(count++ + "*" ); else Console.Write(count++); } Console.WriteLine(); } count = count - N; // This is lower half of pattern for (i = N; i >= 1; i--) { // for loop to display number // upto i for (j = 1; j <= i; j++) { if (j < i) Console.Write(count++ + "*" ); else Console.Write(count++); } count = (count + 1) - 2 * i; Console.WriteLine(); } } public static void Main() { int N = 4; // Function call pattern(N); } } |
Javascript
// JavaScript program to print the half diamond // pattern using numbers and stars // Function to print half diamond // pattern using numbers and stars function pattern(N) { let i, j, count = 1; // This is upper half of pattern for (i = 1; i <= N; i++) { // for loop to display number upto i let row = "" ; for (j = 1; j <= i; j++) { if (j < i) { row += count++ + "*" ; } else { row += count++; } } console.log(row); } count = count - N; // This is lower half of pattern for (i = N; i >= 1; i--) { // for loop to display number upto i let row = "" ; for (j = 1; j <= i; j++) { if (j < i) { row += count++ + "*" ; } else { row += count++; } } count = (count + 1) - 2 * i; console.log(row); } } // Driver code let N = 4; // Function call pattern(N); |
1 2*3 4*5*6 7*8*9*10 7*8*9*10 4*5*6 2*3 1
Time Complexity: O(N2)
Auxiliary Space: O(1) since we are not using any extra space
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!