Write a program to print this trangle
1
12
123
1234
12345
123456
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace pascals_triangle
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter the triangle length value, that should be in a number form");
int n=Convert.ToInt32(Console. ReadLine());
bool a;
a=Convert.ToBoolean(n);
if (a==true)//if "a" value is above zero then it is true otherwise false
{
for (int l = 1; l <= n; l++)//this loop is for sending the prompt to the next line
{
for (int i = 1; i <= l; i++)//this loop is for printing the numbers in a line
{
Console.Write(i);//we used write method to print numbers in a single line
}
Console.WriteLine(" ");//we used writeline method to send the prompt to next line
}
}
else
{
Console.Write("Enter the currect value, Which is in Numeric and Above the value of ZERO");
}
Console.ReadLine();
}
}
}
1. /*straight triangle*/
using System;
namespace amar
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter the value of n");
int n = Convert.ToInt32(Console. ReadLine());
for (int i = 1; i < n; i++)//i valuse assins 1...10/*
{
Console.WriteLine("*");//print stars into next line
for (int j = i; j >= 1; j--)//i value assined to j, j exicutes until it's value becomes 1
{
Console.Write("*");//print stars in same line
}
}
Console.WriteLine("*");
Console.ReadLine();
}
}
}
2. /*reverse triangle*/
using System;
namespace amar
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter the value of n");
int n = Convert.ToInt32(Console. ReadLine());
for (int i = n; n >= 1; n--)
{
for ( int j=1; j< n;j++)
{
Console.Write("*");
}
Console.WriteLine("*");
}
Console.ReadLine();
}
}
}
original reverse triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int n1, i, i1;
Console.WriteLine("enter number of lines to print");
n1 = Convert.ToInt32(Console. ReadLine());
if (n1 <= 1)
{
Console.WriteLine(" plesze enter above on one otherwise the trianglewill not appear");
}
else
{
int m1 = n1 - 1;
int n = n1 + m1;
for (i = n1; i >= 0; i--)
{
for (i1 = 1; i1 <= n1 - i; i1++)
{
Console.Write(" ");
}
int p, q, i2;
for (i2 = 1, p = i, q = p - 1; i2 <= p + q; i2++)
{
Console.Write("*");
}
int i3;
for (i3 = 1; i3 <= n1 - i - 1; i3++)
{
Console.Write(" ");
}
Console.WriteLine(" ");
}
}
Console.ReadLine();
}
}
}
original triangle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
int n1, i, i1;
Console.WriteLine("enter number of lines to print");
n1 = Convert.ToInt32(Console. ReadLine());
if (n1 == 1)//if the user given value will be 1 then, this condition exicutes
{
Console.Write(" ");
Console.Write("*");
Console.Write(" ");
}
else
{
/* if we consider the triangle as squre,
* then they are 2 symboles are there.
* they are "*" and " "(space).
* in that the value of star in last line ended with 3,5,7....
* for these numbers we written this 2 lines below */
int m1 = n1 - 1;//if given one value that is dicreased by 1 and the value sended to the m1
int n = n1 + m1;//if the value in m1 is added with the value what we given(n1),then we get the value as i erliar specified(3,5,7..)
for (i = 1; i <= n1; i++)//each value in "i" gives the single line until the condition is false
{
/*consider your value is 4(n1).
* in it if you take 1st line(i),
* they are 3 speces are required(i1)*/
for (i1 = 1; i1 <= n1 - i; i1++)//this loop is for space before the star symbol
{
Console.Write(" ");
}
int p, q, i2;
for (i2 = 1, p = i, q = p - 1; i2 <= p + q; i2++)//this loop is for printing *'s which are in middle
{
Console.Write("*");
}
int i3;
for (i3 = 1; i3 <= n1 - i - 1; i3++)//this loop is for space after the star symbol
{
Console.Write(" ");
}
Console.WriteLine(" ");//printing and moving pointerr to next line
}
}
Console.ReadLine();
}
}
}
No comments:
Post a Comment