0

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?

8
Contributors
16
Replies
17
Views
6 Years
Discussion Span
Last Post by dwarvenassassin
Featured Replies
  • What about this kind of loop: [CODE] int temp = 0; for (int i = 1; i <= 10; i++) { for (int j = 0; j < i; j++) { Console.Write(++temp); } Console.WriteLine(); } Console.ReadLine(); [/CODE] Read More

  • :) Simple isnt it? Im glad you like it. best regards, bye Read More

0

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

0
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:)

Edited by Nick Evan: Fixed formatting

0

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.

Edited by Nick Evan: Fixed formatting

0

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

0

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.. :)

0

// 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:)

0

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!

0

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();

1

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();
0

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

0

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

0
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.

Edited by Nick Evan: Fixed formatting

0

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();
        }
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.