So, my assignment wants me to take an integer and print how ever many numbers until it reaches that number.

So basically, if you type 5, the output would be : 1,2,3,4,5.

My logic was this

Define a for loop that starts at 1 and iterates until i is equal to the parameter.
In the loop use WriteLine and a format specifier to print out the value of the for loop variable followed by a period.
Then print result.

I am not sure how to code this based off of my thinking (DON"T SAY I DIDN"T TRY), can someone assist me code this or refer me to a reference?

Recommended Answers

All 12 Replies

okay so I understand how to use the top 2 links but how would I even use the "convert string to integer"?

I your case you probably don't need to convert a string to an integer. Console.WriteLine or Write take care of converting your int loop variable to a string, if needed.

commented: Thanks +7
commented: actually it is needed to set the limit of the for loop -2

I tried doing this:

string input = string.Empty;
            int number = 0;
            Console.WriteLine("Type in an integer.");
            input = Console.ReadLine();
            number = int.Parse(input);
            int.TryParse(input, out number);
            Console.WriteLine("Thanks for the number {0}", number);

            int x = 0;

            for (x = 0; x++; x < number)
                {
                    Console.WriteLine(x);
                } 

Sorry if my code is retardedly written, I am trying to learn the material.

Anyways, how would I fix my loop so that it will write out a specific amount of numbers depending on the input?

You're mixing some things up. Your code should have given you errors.
A for loop consists most often out of an initialisation( x = 0 ), followed by a condition(boolean) like ( x < number ) and finally an increment or decrement of the loop variable ( x++ )
I would use Write instead of WriteLine, to put all the numbers on the same line, if you wish.
You will notice that still some problems exist. Try to solve them, if in trouble, only one address : DANIWEB!

commented: THANK YOU MASTER ddanbe! +0

Okay so I fixed that up.
Here is what i have:

string input = string.Empty;
            int number = 0;
            Console.WriteLine("Type in an integer.");
            input = Console.ReadLine();
            number = int.Parse(input);
            int.TryParse(input, out number);
            Console.WriteLine("Thanks for the number {0}", number);

            int x = 0;

            for (x = 1; x < number; x++ )
            {
                Console.WriteLine(x);
            } 

How come the last number doesn't display?

If i input 9, it would output 8 (as an example).

Thank you so far!

Your code is along the right track, however a few things need changing. Here is a revised code for you:

        static void Main(string[] args)
        {

            string input = "";
            int number = 0;
            bool Done = false;
            //Whenever getting input from a user it's usually a good idea to validate the input and get it corrected if needed, before continuing.
            while (!Done)
            {
                Console.WriteLine("Type in an integer.");
                input = Console.ReadLine();
                Done = int.TryParse(input, out number);
            }
            Console.WriteLine("Thanks for the number {0}", number);
            //This sets the loop to print all the numbers except the last one so that  a '.' can be printed after the last number instead of a ','
            for (int x = 1; x < number;x++)
            {
                Console.Write(x + ",");
            }
            //This prints the last number, the period, a new line character and the closing message.
            Console.WriteLine(number + ".\nPress Any Key To Continue");
            Console.ReadKey();
        }

Look at the condition (x < number)!
Say x has incremented to the value 8. 8 < 9 is true so your loop executes and prints 8. Now x will increment to 9 and 9 < 9 is false, so your loop ends. Change < to <=.

commented: ThAnKs ddanbe! +0

OKAY! I figured it out, i had to put a +1 next to x in line 13.

THANKS GUYS!!

ddanbe, your solution with the x<= works. Thanks!

Glad you got your solution, please remember to mark this solved thanks.

commented: gladly +0

sure thing, thanks for assisting me!

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.