This is a C# program to print the pyramid. This program uses the for loops to print the pyramid.
These program display Pyramid of numbers and stars. These codes illustrate how to create Pyramid using c# programming. Most of these c# programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence.
The trick to the triangle is to use a loop within a loop.
So, we need to get input from user, through console how many row he want to display..In main method we will get input from user and pass that values into our print method. so, there we have 4 for loops.just go through the source code if you have any doubts...!!Here is source code of the C# Program to Display Number in Pyramid Shape. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :So here is our code :
using System; class pyramid { public static void Main() { int num , space; while(true) { Console.Write("Enter a number between 1 to 9 : "); num=Convert.ToInt32(Console.ReadLine()); space=num-1; for(int i=1; i<=num; i++) { for(space=1; space<=(num-i); space++) { Console.Write(" "); } for(int j=1; j<=i; j++) { Console.Write(j); } for(int k=(i-1); k>=1; k--) { Console.Write(k); } Console.WriteLine(); } } } } OUTPUT : Enter a number between 1 to 9 : 5 1 121 12321 1234321 123454321How to make a pyramid using * and 'space' in C#? The output will be like this.
Here is source code of the C# Program to Display Star in Pyramid Shape. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below :using System; class pyramid { public static void Main() { int num , space; while(true) { Console.Write("Enter a number between 1 to 9 : "); num=Convert.ToInt32(Console.ReadLine()); space=num-1; for(int i=1; i<=num; i++) { for(space=1; space<=(num-i); space++) { Console.Write(" "); } for(int j=1; j<=i; j++) { Console.Write("*"); } for(int k=(i-1); k>=1; k--) { Console.Write("*"); } Console.WriteLine(); } } } } OUTPUT : Enter a number between 1 to 9 : 5 * *** ***** ******* *********I'm created this program in Notepad and run in Visual Studio Command Prompt . You can use anything, Console Application or Notepad .Now , you think which one is better ?
- I think notepad is better , because you can learn the namespaces , syntax , methods very fast but in visual studio , it will show automatically all the namespace , syntax , methods .
How to run a program in Command Prompt ?
- First , Type your program in notepad then save the notepad file with .csextension .
- Then, open Visual Studio Command Prompt and select the location of file .
- Compile the program using csc abc.cs ( Type csc (C# Compiler) and your file name ) , abc.cs ( File Name ) .
- Execute your program , abc ( Only type your file name without extension ) .
A
ReplyDeleteAB
ABC
ABCD
ABCDE
{
Deleteint i, j;
char c;
for (i = 0; i < num; i++)
{
c = 'A';
for (j = 0; j <= i; j++)
{
Console.Write(c);
c++;
}
Console.WriteLine(" ");
}
Console.ReadKey();
}
int num, space;
Deletewhile (true)
{
Console.WriteLine("Please Enter No of rows");
num = Convert.ToInt32(Console.ReadLine());
space = num - 1;
for (int i = 1; i <= num; i++)
{
for (space = 1; space <= (num - 1); space++)
{
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
for (int k = (i - 1); k >= 1; k--)
{
Console.Write(k);
}
Console.WriteLine();
}
}
how can do this in c#
ReplyDeleteusing System;
ReplyDeleteclass program
{
public static void Main()
{
for (int i = 1; i <= 5; i++)
{
int no = 65;
for (int j = 1; j <= i; j++)
{
char ch = (char)no;
Console.Write(ch);
no++;
}
Console.WriteLine();
}
Console.ReadLine();
}
}
to print
ReplyDelete*
***
**
*
1234567890
ReplyDelete357924689
83726158
2198764
318641
49515
4566
923
25
7
How to do this??
*1234
ReplyDelete**123
***12
****1
*****
hi guys,
ReplyDeletei need the output in c # like this
*
* *
* * *
* * * *
* * *
* *
*
pls help me to do above pattern.
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehi i want to print the following number pattern
ReplyDelete1
12
123
1231
12312
123123
plz solve this
int i, j;
Deletefor (i = 1; i <=15; i++)
{
for (j = 1; j <=i; j++)
{
Console.Write("" + (j));
}
Console.WriteLine("");
}
Console.ReadKey();
I also want same soln
Deletein c# .net
ReplyDeleteThis comment has been removed by the author.
ReplyDelete9
ReplyDelete9 7 9
9 7 5 7 9
9 7 5 3 5 7 9
9
ReplyDelete9 7 9
9 7 5 7 9
9 7 5 3 5 7 9
*
ReplyDelete*@+
*@+*
*@+*@ please solve this
how to print
ReplyDeleteA
ABA
ABCBA
how to print
ReplyDelete1
123
12345
123
1
*********
*******
*****
***
*
how to print this pattern
ReplyDelete*
***
*****
***1***
please tell me fast
How to print this pattern
ReplyDeleteWrite a C# program to generate the following pattern:
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
1
ReplyDelete21
321
4321
This pattren please.........\
ReplyDelete12345
21234
32123
43212
54321
1
ReplyDelete234
56789
8765432
Give logic for this
nyce
ReplyDelete11
ReplyDelete5
ReplyDelete4 4
3 5 3
2 4 4 2
1 3 5 3 1
can anyone solve this plz help me and mail to this
mailid:kolasaicharan99@gmail.com
*
ReplyDelete* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Can anyone write a program like above
This comment has been removed by the author.
ReplyDeletehow can I make this:
ReplyDelete1
23
456
78910
and so on using user input about the end number and not rows?
How to Write
ReplyDelete1*2*3*10*11*13
*4*5*8*9
**6*7
1
ReplyDelete313
53135
7531357
975313579
solve it