C# Program to Print a Diamond Pattern using Nested Loop

«
»

This is a C# Program to print a diamond using nested loop.

Problem Description

This C# Program Prints a Diamond Using Nested Loop.

Problem Solution

Take input from the user and go on to draw the diamond pattern as shown in the program below.

Program/Source Code

Here is source code of the C# Program to Print a Diamond Using Nested Loop. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Print the Sum of all the Multiples of 3 and 5
 */
using System;
class program
{
    public static void Main()
    {
        int number, i, k, count = 1;
        Console.Write("Enter number of rows\n");
        number = int.Parse(Console.ReadLine());
        count = number - 1;
        for (k = 1; k <= number; k++)
        {
            for (i = 1; i <= count; i++)
                Console.Write(" ");
            count--;
            for (i = 1; i <= 2 * k - 1; i++)
                Console.Write("*");
            Console.WriteLine();
        }
        count = 1;
        for (k = 1; k <= number - 1; k++)
        {
            for (i = 1; i <= count; i++)
                Console.Write(" ");
            count++;
            for (i = 1; i <= 2 * (number - k) - 1; i++)
                Console.Write("*");
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}
Program Explanation

In this C# program, we are reading the number of rows using ‘number’ variable. Compute the value of ‘count’ variable as the difference between the values of ‘number’ variable by 1.

advertisement
advertisement

Using for loop initialize the value of ‘k’ variable as 1 and check the condition that the value of ’k’ variable is less than or equal to the value of ‘number’ variable. If the condition is true then execute the iteration of the loop.

Another for loop is used to check that the value of ‘i’ variable is less than or equal to the value of ‘count’ variable. If the condition is true, then execute the iteration of the loop and decrement the value of ‘count’ variable by 1.

Using another for loop initialize the value of ‘i’ variable as 1 and check the multiplication of the value 2 with the difference of the value of ‘k’ variable and the difference of resulted value by1, are equal using logical AND operator. If the condition is true, then execute the iteration of the loop. Print the * Symbol.

Another for loop is used by initialize the value of ‘k’ variable as 1. Check the value of ‘k’ variable is less or equal to the value of ‘number’ variable. If the condition is true then it will execute the iteration of the loop.

Another for loop is used to check the value of ‘i’ variable is less than or equal to the value of ‘count’ variable. If the condition is true then execute the iteration of the loop and decrement the value of ‘count’ variable by 1.

Using another for loop initialize the value of ‘i’ variable as 1. Check the multiplication of 2 values with the difference of the value of ‘number’ variable by the value of ‘k’ variable and also by1. If the condition is true then execute the iteration of the loop and print the * symbol.

advertisement
Runtime Test Cases
 
Enter number of rows
3
  *
 ***
*****
 ***
  *

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.