Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern.
*
**
***
****
*****
******
*****
****
***
**
*
Examples:
Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** *
Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print then separately with the help of the loops. The key observation for printing the upper half and lower half is described as below:
- Upper half: The upper half of the pattern contains star ‘*’ in increasing order where ith line contains following number of star:
Number of '*' in ith line =
- Lower Half: The lower half of the pattern contains star ‘*’ in decreasing order where ith line contains following number of star:
Number of '*' in ith line =
- Program to print half diamond Number-Star pattern
- Program to print Inverse Diamond pattern
- Program to print numbers with diamond pattern
- Program to print hollow pyramid and diamond pattern
- Program to print Hut Star pattern
- Program to print number with star pattern
- Print the Alphabets A to Z in Star Pattern
- Python | Print an Inverted Star Pattern
- Program for diamond pattern with different layers
- Program for Arrow Star Pattern
- Program to print the diamond shape
- Python Program to print a number diamond of any given size N in Rangoli Style
- Program to print diagonal star patterns
- Program to print hollow rectangle or square star patterns
- Program to print the pattern "GFG"
Below is the implementation of the above approach:
C++
// C++ implementation to print the // half diamond star pattern #include <iostream> using namespace std; // Function to print the // half diamond star pattern void halfDiamondStar(int N) { int i, j; // Loop to print the upper half // diamond pattern for (i = 0; i < N; i++) { for (j = 0; j < i + 1; j++) cout << "*"; cout << "\n"; } // Loop to print the lower half // diamond pattern for (i = 1; i < N; i++) { for (j = i; j < N; j++) cout << "*"; cout << "\n"; } } // Driver Code int main() { int N = 5; // Function Call halfDiamondStar(N); } |
Java
// Java implementation to print the // half diamond star pattern import java.util.*; class GFG{ // Function to print the // half diamond star pattern static void halfDiamondStar(int N) { int i, j; // Loop to print the upper half // diamond pattern for (i = 0; i < N; i++) { for (j = 0; j < i + 1; j++) System.out.print("*"); System.out.print("\n"); } // Loop to print the lower half // diamond pattern for (i = 1; i < N; i++) { for (j = i; j < N; j++) System.out.print("*"); System.out.print("\n"); } } // Driver Code public static void main(String[] args) { int N = 5; // Function Call halfDiamondStar(N); } } // This code is contributed by Rohit_ranjan |
C#
// C# implementation to print the // half diamond star pattern using System; class GFG{ // Function to print the // half diamond star pattern static void halfDiamondStar(int N) { int i, j; // Loop to print the upper half // diamond pattern for(i = 0; i < N; i++) { for(j = 0; j < i + 1; j++) Console.Write("*"); Console.Write("\n"); } // Loop to print the lower half // diamond pattern for(i = 1; i < N; i++) { for(j = i; j < N; j++) Console.Write("*"); Console.Write("\n"); } } // Driver Code public static void Main(String[] args) { int N = 5; // Function Call halfDiamondStar(N); } } // This code is contributed by Rohit_ranjan |
* ** *** **** ***** **** *** ** *
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Rohit_ranjan

