Hi,
I am new to programming and have just started learning C and C#. I am trying to print the following pattern using for loop in C#

1
23
456
78910
and so on...

I have tried everything but its not giving the desired output. Can anyone help me with this?

Hi, andur92, welcome to this site. :)
Sure we can help!
Have you tried something for yourself so far?

int[] nums = { 1, 23, 456, 78910 };


// loop through int array with for loop
for (int i = 0; i < nums.Length; i++)
{
// Assign string reference based on induction variable
string value = nums.ToString();
// Display each variable in array
MessageBox.Show(value);
}
}

Hope this helps. If so please mark as answered and please give rep point:)

Hi ddanbe!
Yes I have been trying this for the past two days with continuous output which is nothing but failure. Following is the code I wrote for printing the following pattern:

1
12
123
1234

and so on

This is the code:

using System;
class Pattern
{
public static void Main()
{
int row,col;
for (row=1;row<11;row++)
{
for (col=1;col<row;col++)
{
Console.Write(col);
}
Console.WriteLine(row);
}
}
}

I tired manipulating this code numerous times but was unsuccessful every single time, resulting in weird patterns. I tried using three FOR loops even but was still stuck.

Seriously?Guys the code I just presented works 100%. I tested it. It works, STOP typing, take my code, tweak it and give me a rep point dammit lol :) Tired of the word Newbie under my name haha

Hi syncmaster!
Yes that will produce the output I am sure but we have not been taught most of the things you wrote in the code, yet! We were asked just to use the FOR loop. But still thanks for your help.. :)

// declaring int array(there is multiple ways to declare an array)
//in this case, you are declaring and int array with 4 values in it
int[] nums = { 1, 23, 456, 78910 };

// loop through int array with for loop
for (int i = 0; i < nums.Length; i++)
// i = 0, i < 4, increment i by 1 after loop
{
// Assign string reference based on induction variable
string value = nums.ToString();
// Display each variable in array
MessageBox.Show(value);
}
Hope that eases your guys mind a lil bit more:)

Synmaster, yes i get that. but we aren't suppose to use MessageBox and other things as we haven't been taught these things yet. Still man I appreciate your effort for helping me out!

well maybe you should have said that in the beginning. The better you explaing your newly posted thread, the easier it is on the rest of us to help you out!
but im then assuming your still in the cosole application development stage. You would then use Console.WriteLine();

What about this kind of loop:

int temp = 0;
            for (int i = 1; i <= 10; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    Console.Write(++temp);
                }
                Console.WriteLine();
            }
            Console.ReadLine();

Omg! This is it! Mitja Bonca!! You are a genius! Thank you man! :) :) :) :)

:)
Simple isnt it?
Im glad you like it.

best regards,
bye

commented: You are awesome! +1

@mitja You have just posted the best pattern, but i have a question that can we add some more variables in this program ?

int[] nums = { 1, 23, 456, 78910 };
Int64 numbers = 0;
Console.WriteLine("enter the set of numbers");
numbers = Convert.ToInt64(Console.ReadLine());


string str = numbers.ToString();
string strOutput = string.Empty;
int iSep = 1;
// loop through int array with for loop
for (int i = 0; i < str.Length; i++)
{
strOutput = str.Substring(0, iSep);
Console.WriteLine(strOutput);
if(str.Length!=0)
str = str.Remove(0, iSep);


iSep++;


}
Console.ReadLine();

try with this.

1for loop used in the program and disply the following output in c#
*
***
*****
*******

Actually not as easy as it first looks...

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int valueToOuput = 0;
            int numberOfNumbers;
            for(numberOfNumbers = 1; numberOfNumbers<20; numberOfNumbers++)
            {
                string output = "";
                int sum = 0;
                for (int i = 0; i < numberOfNumbers; i++)
                {
                    sum = ++valueToOuput;
                    output += sum.ToString();
                }
                valueToOuput = sum;
                Console.WriteLine(output);
            }
            Console.ReadLine();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.