Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Other Compiled Languages (https://www.askmehelpdesk.com/forumdisplay.php?f=466)
-   -   How to print binary numbers triangle in c sharp? (https://www.askmehelpdesk.com/showthread.php?t=463804)

  • Apr 10, 2010, 07:21 AM
    damaxy
    How to print binary numbers triangle in c sharp??
    I am trying to write a C sharp program to display something like this
    1
    01
    101
    0101
    But I'm having trouble doing that.So far I can display only like this
    1
    10
    101
    1010
    I know its simple for you guys.. I need help... anyone?? Thanks in advance...
  • Apr 10, 2010, 07:24 AM
    damaxy

    oh! My programs code is as the following:
    static void Main(string[] args)
    {
    String input;
    int I, j, ran;
    Console.WriteLine("Enter the Range:");
    input = Console.ReadLine();
    ran = Convert.ToInt16(input);

    for (I = 1; I <= ran; I++)
    {
    for (j = 1; j <= I; j++)
    {
    if (j % 2 == 0)
    {
    Console.Write("0 ");
    }
    else
    {
    Console.Write("1 ");
    }
    }
    Console.WriteLine();
    }
    Console.Read();
    }
  • Feb 5, 2011, 11:33 PM
    ashif
    The simple way

    answer
    using System;
    class b2
    {
    public static void Main()
    {

    int I,j,k;
    Console.WriteLine("Enter the Range");
    k=int.Parse(Console.ReadLine());
    for (I=1;i<=k;i++)
    {
    for (j=1;j<=i;j++)


    if(j % 2==0)
    Console.Write("0");
    else
    Console.Write("1");
    Console.Write("\n");
    }
    }
    }
  • Sep 11, 2011, 05:09 AM
    sujeet1
    static void Main(string[] args)
    {


    int a = 1;
    int b = 0;
    int e = 1;
    int f = 0;
    int m = 0;

    Console.WriteLine("enter the number");
    int s = int.Parse(Console.ReadLine());
    for (int I = 1; I <= s; I++)
    {
    for (int j = 1; j <= I; j++)
    {
    Console.Write(a);
    int d = a;
    a = b;
    b = d;
    }
    Console.WriteLine();
    a = e;
    b = f;
    int h = a;
    a = b;
    b = h;
    m = e;
    e = f;
    f = m;
    }

    Console.ReadLine();
    }
  • Sep 11, 2011, 11:38 PM
    sujeetpasi
    static void Main(string[] args)
    {

    int a = 0;
    int b = 1;
    Console.WriteLine("enter the hieght");
    int j = int.Parse(Console.ReadLine());
    for (int I = 2; I <= j + 1; I++)
    {
    if (I % 2 == 0)
    {
    a = 1;
    b = 0;
    }

    else
    {
    a = 0;
    b = 1;
    }
    for (int k = 1; k <= I - 1; k++)
    {
    Console.Write(a);
    int c = a;
    a = b;
    b = c;
    }
    Console.WriteLine();
    }
    Console.ReadLine();
    }

    please reply wheather this post was helpful to you

  • All times are GMT -7. The time now is 11:20 PM.