-2
\$\begingroup\$

I am working through a worksheet of for loops in c# and have come across this problem,

"Write a program to request an integer n from 1 to 9 and print a line of output consisting of ascending digits from 1 to n followed by descending digits from
n – 1 to 1. For example, if n = 5, print the line 12345432".

This is my code so far

        int n;
        Console.Write("Enter A Number From 1 To 9: ");
        n = Convert.ToInt32(Console.ReadLine());
        if (n > 9 || n < 0)
        {
            Console.Write("Enter A Valid Number: ");
            n =Convert.ToInt32(Console.ReadLine());
        }

        for (int i = 1; i<=n; ++i)
        {
            Console.Write(i);

        }
        for (int j = n; j >= 1; --j)
        {
            Console.Write(j);
        }

The issue I am having is on my descending output it repeats the original user input value, for instance; n=5

output = 1234554321

I know why this is, it's because I have made j == n, however I cannot seem to think of any other way to achieve the second part of the question where I need to output the descending digits. Any help or suggestions is greatly appreciated!

New contributor
hazey is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

closed as off-topic by Toby Speight, πάντα ῥεῖ, vnp, Sᴀᴍ Onᴇᴌᴀ, user673679 Feb 11 at 20:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Toby Speight, πάντα ῥεῖ, vnp, Sᴀᴍ Onᴇᴌᴀ, user673679
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ assign to a string the output of the first for loop. Then do the same for the second for loop. Then output after both for loops. FYI if it's supposed to be from 1-9 then your if condition checking for less then 0 needs to be <=0 to ensure you get 1-9, because right now you are going to allow 0. \$\endgroup\$ – Caperneoignis Feb 11 at 19:59
  • \$\begingroup\$ so string value =''; then value += i.toString(); in both loops \$\endgroup\$ – Caperneoignis Feb 11 at 20:02

Browse other questions tagged or ask your own question.