0

How do I print this in console:

123456
23456
3456
456
56
6

It's a homework from my programming class and I am new to coding. I know I could use:

    static void Main(string[] args)
    {
        Console.WriteLine("123456");
        Console.WriteLine("23456");
        Console.WriteLine("3456");
        Console.WriteLine("456");
        Console.WriteLine("56");
        Console.WriteLine("6");

    }

But I was wondering if there is a cleaner and more efficient way to do it.

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
  • 3
    use `Console.WriteLine()` instead of `Console.Write()`. Also, please include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) along with what you've tried already. – Jesse Nov 28 '21 at 13:46
  • 1
    welcome to stackoverflow. seems like a typical [homework question](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) to me. can you please share what you've tried and researched so far? we'll gladly help if you're stuck somewhere, but you should first attempt to solve the task on your own - and be able to describe your efforts. i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Nov 28 '21 at 13:51
  • 1
    also: it seems that you're not removing the _trailing_, but the _leading_ digit. – Franz Gleichmann Nov 28 '21 at 13:52
  • 1
    Looks like a great case for a set of NESTED for loops. The outer for loop will go from 1 to 6. The inner for loop will go from the current outer for loop value up to 6. Within the inner for loop output each digit with a `Console.Write()` so it stays on the same line. Between the two closing brackets of your nested for loops use a `Console.WriteLine()` to move down to the next line. – Idle_Mind Nov 28 '21 at 13:53
  • What other guidelines and/or restrictions are listed in the homework description? There are many, many ways one could do this... – Idle_Mind Nov 28 '21 at 13:55
  • I am very sorry. I have updated my question to what I could do best right now and I couldn't find an answer around google but I am not sure of how to best describe it – Mohamed Jeylani Nov 28 '21 at 13:58
  • Your description says to use `loops`. Where in your code are you using a loop? – Idle_Mind Nov 28 '21 at 13:59
  • There are no restrictions whatsoever, only that the output is as I have posted – Mohamed Jeylani Nov 28 '21 at 13:59
  • 1
    Look into the Substring function on a string instance – Hans Kesting Nov 28 '21 at 14:03
  • 1
    `string source = "123456"; Console.Write(string.Join(Environment.NewLine, Enumerable.Range(0, source.Length - 1).Select(i => source.Substring(i))));` – Dmitry Bychenko Nov 28 '21 at 15:22

4 Answers4

2
    string source = "123456";
    Console.WriteLine(source);
        while(source.Length > 0) {
            source = source.Remove(0, 1);
            Console.WriteLine(source);
        }

In detail:

while: meanwhile the condition (the lenght of the string, in chars) be greater than zero, do everything between brackets.
Then replace the original string with the same string but removing the first chracter with Remove. 0 means: position where I begin to remove, 1 means: how many characters I will remove. Everytime you Remove a char, the lenght will be close to zero, until be zero and then the while will not be true anymore

Test it: https://dotnetfiddle.net/7tFV0J

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
2

Well, a simple, very efficient way of producing the required output would be the following single line of code:

Console.WriteLine("123456\n23456\n3456\n456\n56\n6");

However, I doubt that will satisfy your homework requirement. A better way, using a loop, is to call the string.Remove() function, and keep doing so until the string is empty:

using System;

namespace StringRemove
{
    class Program
    {
        static void Main()
        {
            string test = "123456";
            while (test.Length != 0) {
                Console.WriteLine(test);
                test = test.Remove(0,1);
            }
        }
    }
}

For an in-depth discussion on removal of the first character from a string, see: Fastest way to remove first char in a String.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
2
string digits = "123456";
for(var i = 0; i < digits.Length; i++)
   Console.WriteLine(digits.Substring(i));
lidqy
  • 1,891
  • 1
  • 9
  • 11
1

Here's an example using nested for loops as mentioned in the comments:

public static void Main (string[] args) { 
    for(int i=1; i<=6; i++) {
        for(int j=i; j<=6; j++) {
            Console.Write(j);
        }
        Console.WriteLine();
    }
}

Output:

123456
23456
3456
456
56
6
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40