triangle paramid using for loop in C# [duplicate] - c#

This question already has an answer here:
C# program to develop a pyramid, using for loop [closed]
(1 answer)
Closed 6 years ago.
i can't solve my triangle program in school , it is printing one sided curve by this program
using System;
namespace starpyramid
{
class program
{
static void Main()
{
Console.Write("Height: ");
int i = int.Parse(Console.ReadLine());
if(i>0)
{
goto main;
}
else
{
Main();
}
main:
Console.Write("\n");
for (int h = 1; h<= i;h++) // main loop for the lines
{
for (int s = h; s <= i; s++) //for spaces before the stars
{
Console.Write(" ");
}
for(int j=1; j<(h*2); j=j+2)
{
Console.Write("*");
}
Console.Write("\n");
}
}
}
}
but i need to modify it by something that make this a proper triangle !

Here is one solution:
public static void DrawPyramid(int Rows)
{
string Pyramid = string.Empty;
int n = Rows;
for (int i = 0; i <= n; i++)
{
for (int j = i; j < n; j++)
{
Pyramid += " ";
}
for (int k = 0; k < 2 * i - 1; k++)
{
Pyramid += "*";
}
Pyramid += Environment.NewLine;
}
Pyramid.ConsoleWrite();
}
Example:
DrawPyramid(5);
// *
// ***
// *****
// *******
// *********

Related

C# christmas tree

I'm novice in C# and cause I request help me to implement this:
*
*
***
*
***
*****
*
***
*****
*******
*
***
*****
*******
*********
I just had this code:
class Program
{
static void Main(string[] args)
{
AnotherTriangle ob = new AnotherTriangle();
ob.CreateTriangle();
Console.ReadLine();
}
}
class AnotherTriangle
{
int n;
string result = "*";
public void CreateTriangle()
{
flag1:
Console.Write("Please enter number of triangles of your tree: ");
n = int.Parse(Console.ReadLine());
Console.WriteLine();
if (n <= 0)
{
Console.WriteLine("Wrong data type\n"); goto flag1;
}
string s = "*".PadLeft(n);
for (int i = 0; i < n; i++)
{
Console.WriteLine(s);
s = s.Substring(1) + "**";
for (int j = 0; j < n; j++)
{
Console.WriteLine(s);
}
}
}
}
At now I have only "tower", not equilateral triangle. Please help.
Console.WriteLine("Please enter the number of triangles in your tree: ");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
string branch = new String('*', j);
Console.WriteLine(branch.PadLeft(n + 3) + "*" + branch);
}
}
A working example.

Print Multiplication tables from 2 to 12 in c#

class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Multiplication Tables");
for (int i = 2; i <= 12; i++)
{
for (int j = 1; j <= 10; j++)
{
Console.WriteLine("{0}*{1}={2}", i, j, i*j);
}
Console.ReadLine();
}
}
}
I want to print multiplication tables from 2 to 12, with the above code I am able to print only one table. I didn't get why the first loop counter was not incrementing.
Any help appreciated.
It's waiting for input after each inner loop.
Remove:
Console.ReadLine();
From your outer loop, and add it to the end.
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Multiplication Tables");
for (int i = 2; i <= 12; i++)
{
for (int j = 1; j <= 10; j++)
{
Console.WriteLine("{0}*{1}={2}", i, j, i*j);
}
}
Console.ReadLine(); // <-- Both loops now complete
}
}
/* multiply of 2 to 12;*/
using System;
namespace multiply
{
class Program
{
static void Main(string[] args)
{
int a, b;
for (a=1; a <= 12; a++) {
Console.Write("\n");
for (b = 1; b <= 10; b++)
{
Console.Write("\n "+a* b);
}
}
Console.Read();
}
}
}

Making a star pyramid in C#

So I'm trying to do a pyramid in C# but I can't get it to print properly.
I'm doing this for my school's C# class, can't really figure out how to get the spaces in the pyramid properly. I feel like I'm really doing something wrong here.
Instead of a triangle I'm getting something like this:
*
**
***
****
*****
******
*******
********
Here is my code:
using System;
namespace Pyramidi
{
class Ohjelma
{
static void Main()
{
int korkeusMax = 0;
int valit = 0;
do
{
Console.Write("Anna korkeus: ");
korkeusMax = Convert.ToInt32(Console.ReadLine());
if (korkeusMax > 0) {
break;
}
else {
continue;
}
}
while(true);
for (int korkeus = 1; korkeus <= korkeusMax; korkeus++)
{
valit = (korkeusMax - korkeus) / 2;
for (int i = 0; i < valit; i++)
{
Console.Write(" ");
}
for (int leveys = 1; leveys <= korkeus; leveys++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Try this:
for (int korkeus = 0; korkeus < korkeusMax; korkeus++)
{
for (int i = 0; i < (korkeusMax - korkeus - 1); i++)
{
Console.Write(" ");
}
for (int i = 0; i < (korkeus * 2 + 1); i++)
{
Console.Write("*");
}
Console.WriteLine();
}
Alternatively, instead of loops, you can use new String('*', num). Try this:
for (int korkeus = 0; korkeus < korkeusMax; korkeus++)
{
Console.Write(new String(' ', korkeusMax - korkeus - 1));
Console.Write(new String('*', korkeus * 2 + 1));
Console.WriteLine();
}
You are doing the first half right, but what you want to do for the second half is another for loop that does the opposite.
So you probably want to copy your for loop and change the header from
for (int korkeus = 1; korkeus <= korkeusMax; korkeus++)
to
for (int korkeus = kokeusMax; korkeus > 1; korkeus--)
or something along those lines

C# Multiplication Table

So I'm attempting to print a multiplication table in C# however I can't quite figure out how to get what I need.
So far my program outputs the following:
1 2 3
2 4 6
3 6 9
However, I need it to output this:
0 1 2 3
1 1 2 3
2 2 4 6
3 3 6 9
I've tried a lot of different ways to get the second output however I can't quite figure it out. I'm not necessarily asking for an answer but if someone could point me in the right direction it would be much appreciated.
This is the code I have as of now:
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
Console.Write(i * j + "\t");
}
Console.Write("\n");
}
Console.ReadLine();
}
for (int i = 0; i <= 3; i++)
{
Console.Write(i + "\t");
for (int j = 1; j <= 3; j++)
{
if (i>0) Console.Write(i * j + "\t");
else Console.Write(j + "\t");
}
Console.Write("\n");
}
int tbl= int.Parse(Console.ReadLine());
int j = int.Parse(Console.ReadLine());
for (int i=1; i<=10; i++)
{
for (int j=1;j<=10; j++)
{
Console.WriteLine("{0}*{1}={2}", i, j, (i * j));`enter code here`
}
}
Console.ReadLine();
You should skip both 0's.
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
Console.Write((i == 0? j : (j == 0? i : i*j)) + "\t");
}
Console.Write("\n");
}
You could try one of this three solutions.
Solution 1 (without if else statement):
static void Main(string[] args)
{
for (int i = 0; i <= 3; i++)
{
Console.Write("{0}\t", i);
for (int j = 1; j <= 3; j++)
{
Console.Write("{0}\t", i * j);
}
Console.WriteLine();
}
Console.ReadLine();
}
Solution 2 (With if else statement):
static void Main(string[] args)
{
for (int i = 0; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (i == 0)
{
Console.Write("{0}\t", i);
}
else
{
Console.Write("{0}\t", i * j);
}
}
Console.WriteLine();
}
Console.ReadLine();
}
Solution 3 (With short-hand if else statement):
static void Main(string[] args)
{
for (int i = 0; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
Console.Write("{0}\t", (i == 0) ? i : i * j);
}
Console.WriteLine();
}
Console.ReadLine();
}
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
if (i == 0)
{
Console.Write(j);
}
else
{
if(j == 0)
{
Console.Write(i);
}
else
{
Console.Write(i * j);
}
}
}
Console.Write("\n");
}
Console.WriteLine("Enter A Number");
int j = Convert.ToInt32(Console.ReadLine());
for (int i = 0 ; i <= 10; i++) {
Console.WriteLine("{1} X {0} = {2}",i,j,i*j);
Console.ReadLine();
}
public class Program
{
//int num;
public static void Main(string[] args)
{
int num,num1;
Console.WriteLine("enter a any number num");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter any second number num1");
num1 = Convert.ToInt32(Console.ReadLine());
for(int i = num;i <= num1; i++)
{
for (int j = 1; j <= 10; j++)
{
Console.Write(i *j+ "\t");
}
}
Console.Write("\n");
}
}
using System;
/*
* Write a console-based application that displays a multiplication table of the product of
* every integer from 1 through 10 multiplied by every integer from 1 through 10. Save the
* file as DisplayMultiplicationTable.cs.
*/
namespace MultiplicationTable
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\t\t\t\t\t\t\t\t\tMultiplication Table");
Console.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------");
const int END = 11;
for(int x = 1; x < END; x++)
{
for(int y = 1; y < END; y++)
{
int value = x * y;
Console.Write("{0} * {1} = {2}\t", y, x, value);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output
Output of Code
I am attempting to complete the above code in a GUI. So far I have come up with the following code; but the output is not like the above output.
My Code for the GUI is as follows:
using System;
using System.Windows.Forms;
namespace DisplayMultiplicationTableGUI
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void ShowTableButton_Click(object sender, EventArgs e)
{
int a;
int b;
const int STOP = 11;
for(a = 1; a < STOP; a++)
{
for(b = 1; b < STOP; b++)
{
int value = a * b;
multiplicationTableLabel.Text += String.Format("{0} * {1} = {2} ", b, a, value);
}
multiplicationTableLabel.Text += "\n";
}
}
}
}

Where can I find C# code for console application designs. The star pyramid for example [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want to know if there's a website that provides code star designs for my console application. For example I want a code that can output the pyramid using for loops:
*
***
*****
*********
Or code that can output the half-life logo using for loops.
It doesn't matter where the code was created, as long as I can understand the for loop it is ok.
using System;
using System.Collections.Generic;
using System.Text;
namespace Pyramid
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("Enter the Height of the Pyramid: ");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
for (int j = n; j >= i; j--)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write("*");
}
for (int m = 2; m <= i; m++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} Console.Read();
}
}
}
int height = 5;
for (int count = 1; count <= height; count++)
Console.WriteLine(new String('*', count * 2 - 1).PadLeft(height + count));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PyramidUsingForLoops
{
class StarPyramid
{
static void Main(string[] args)
{
int Row = 5;
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Row-(i+1); j++)
{
Console.Write(" ");
}
for (int k = 0; k < 2*i+1; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
namespace Program
{
class Program
{
static void Main(string[] args)
{
// print triangle
int n = 5;
/* three phrase:
* first: find first location of the line
* second: print increasing
* third: print decreasing */
int k=n;
for (int i = 0; i <n; i++) //print n line
{
// first
for (int j = 1; j <= k; j++) Console.Write(" ");
// second
for (int j = 1; j <= i; j++) Console.Write(j);
// third
for (int j = i + 1; j >= 1; j--) Console.Write(j);
k--;
Console.WriteLine();
}
}
}
}
int rowCount = 5;
for (int i = 0; i < rowCount; i++)
{
Console.Write(new string(' ', rowCount - i - 1));
Console.Write(new string('*', 2 * i + 1));
Console.WriteLine();
}
Regarding your comment to the question: you're saying that one can do "some complex stuff" with the for-loop you're trying to discover. I have to say: you seem to be on the wrong track. The for-loop has always the same simple structure:
for (Type variable = startvalue; condition; action)
{
// Do stuff
}
The "complex" stuff is to sometimes find out what condition is or what action to take. condition may be anything that evaluates to boolean and action may be anything too.
So the "complex" stuff has nothing to do with the structure of the for-loop itself. You could also write the following:
for (int i = 0; DateTime.Now < new DateTime(2009, 12, 31); i++)
{
Console.WriteLine("I've iterated {0} times before 31st December 2009!", i);
}
The condition does not even consider the i variable, but still it is a valid for-loop.

Categories

Resources