Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# printing for loop

Tags:

c#

I want to print this:- 1 121 12321 1234321 123454321

This program prints this. What could be the error in the program ? In the for loop ? Please tell me how to fix this. What is the error in the logic I am using. If so, what is the problem in my logic ? How should I think for algorithms in the near future ? :- 1 12 123 123432 123454321

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c, d, e;
            b = 6;
            for(a=b;a>=1;a--)
            {
                for (c = a; c >=1; c--)
                {
                    Console.Write(" ");
                }
                for(d=1; d<=b-a;d++)
                {
                    Console.Write(d);
                }
                for (e = b-a-1; e>=a; e--)
                {
                    Console.Write(e);
                }
                Console.WriteLine();

            }
        }
    }
}

Please tell me the problem in my code.

like image 670
Rahul Patel Avatar asked Jan 30 '26 21:01

Rahul Patel


2 Answers

the problem is in the line for (e = b-a-1; e>=a; e--)

change it to simply for (e = b-a-1; e>=1; e--)

like image 195
chouaib Avatar answered Feb 01 '26 12:02

chouaib


class Program

{

static void Main(string[] args)

    {
        int a = 1, b, s = 1, n = 5;
        for (; a <= n; s = s * 10 + 1)
        {
            for (b = n - a; b >= 1; b--)
            {
                Console.Write(" ");
            }
            Console.Write(s * s);
            Console.WriteLine();
            a++;
        }

    }
}

Hope this helps.

like image 23
Poonam Singhania Avatar answered Feb 01 '26 11:02

Poonam Singhania



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!