C# .NET - I want This format in c#.net - Asked By Srinivasulu Enugu on 28-Sep-11 12:26 AM

hi all,

       i want code for below format using C#.NET language




                             1
                           2      3
                       4      5      6
                     7      8     9     10


please give me...any body 
Sharad Chandra replied to Srinivasulu Enugu on 28-Sep-11 12:42 AM
void printSpace(int n)
{
    int i;
    for(i = 0; i<n; i++)
    printf(" "); // printing n spaces
}
void printPyramid()
{
    int i, j, sp = 4;
    for(i = 1; i<= 5; i++)
	{ // 5 rows have to print
        printSpace(sp); // printing spaces before numbers
        for(j = 1; j<= 2*i-1; j++)
	{
                printf("%d",j);  // printing 2n-1 number for nth row
        }
        printSpace(sp);  // printing spaces after numbers
        sp--;
        printf("\n");
    }
}
int main()
{
    printPyramid();
    return 0;
}

OUTPUT :

   1
  123
   12345
  1234567
123456789

Sakshi a replied to Srinivasulu Enugu on 28-Sep-11 12:43 AM

Both works :

1. using System; 
using
System.Collections.Generic; 
using
System.Linq; 
using
System.Text; 
 
namespace
Triangle 
{ 
   
class Program 
   
{ 
       
static void Main(string[] args) 
       
{ 
 
           
var triangle = "*"; 
           
do { Console.WriteLine(triangle); } 
           
while ((triangle += "*").Length < 10); 
 
           
Console.ReadLine(); 
       
} 
   
} 
} 

2. Console.WriteLine( 
   
Enumerable.Range(2, 9) 
             
.Aggregate("*", (s, i) => s + Environment.NewLine + new string('*', i))); 
aneesa replied to Srinivasulu Enugu on 28-Sep-11 12:46 AM

Code for Program that displays floyd's triangle of numbers in C Programming

#include <stdio.h>
#include <conio.h>
void main(){
    int i,j,num,c=1;
    clrscr();
    printf("*****FLOYD'S TRIANGLE*****\n\n");
    printf("Enter number upto which u want to generate : ");
    scanf("%d",&num);
    for(i=c;i<=num;i++){
    for(j=1;j<=i;j++,c++){
       printf("%5d",c);
       if(c==num)
          gotoout;
       }
       printf("\n");
    }
    out:
getch();
}
Web Star replied to Srinivasulu Enugu on 28-Sep-11 12:49 AM
Here i put the logic of printing such type of pyramid may be some syntax error but it will print your desired pyramid of number 
//first loop for number or row in your example 4
int num = 4;
int counter = 1;
int counterspace = 0;
for(int i=0; i<num; i++)
{
//2nd loop for print preceding space
for(int j=num - i; j>0; j--)
{
Console.Write(" ");
}
//3rd loop for print number
counterspace   = 1;
for(int k = 0; k<= i; k++)
{
//here print number with space with proper sapce
if((counterspace/2)  > 0)
{
Console.Write(counter);
counter++;
}
else
{
Console.Write(" ");
}
}
}

output as
  1
               2    3
             4    5    6
           7    8   9   10
Anoop S replied to Srinivasulu Enugu on 28-Sep-11 03:41 AM
Refer this code -> How to Print Pyramid in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace printStar
{
class Program
{
static void Main(string[] args)
{ 

Console.WriteLine("Enter the value ");
int k=int.Parse(Console.ReadLine());
int n=k-1;
int x=2*(k-1)+1;
//Outer Loop will run depends
//on the value entered by user 
for(int p=0;p<=n;p++)
{
for(int j=k-1;j>=0;j--)
{
Console.Write(" ");
}
// main loop to print number
for (int i = 0; i <= (x - 2 * (k - 1)); i++) { if (i % 2 == 1) { Console.Write("i"); } else { Console.Write(" "); } } Console.WriteLine(); k--; } Console.ReadLine(); } } }