-1

Everything runs fine. However, I need the loop to end contingent upon how many numbers the user enters in the first question.

using System;

namespace Looping
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many numbers will you provide?");
            int numbersProvide = Convert.ToInt32(Console.ReadLine());

            int i = 0;

            while (i <= numbersProvide)

            {
                Console.WriteLine("Please enter number:");
                int firstNum = Convert.ToInt32(Console.ReadLine());

                int j;
                int sum = 0;
              
                double addDiv;

                for (j = 1; j <= 25; j++)
                {
                    
                    if (firstNum % j == 0)
                    {
                       addDiv = firstNum / j;
                       Console.WriteLine(firstNum + " is divisible by " + j + "(" + addDiv + ")");
                        sum += j;

                    }
                }
                Console.WriteLine("The sum of the quotient is: " + sum);

            }
            Console.ReadKey();
        }
    }
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thanks for responding I will note everything you are saying. The for loop actually gave me what I wanted. I should have mentioned this in the post but I was also concerned with if the user decides to enter more then number after the loop ends it still says "Please enter a number" when it should say "Please enter number 2" and so forth? Any immediate advice? –  May 29 '22 at 17:31

1 Answers1

1

You set int i = 0; and i never increases. Nor does numbersProvide decrease.

Since you have a fixed amount of times that the loopo shall run, a for loop is much more suitable for the situation than the while loop you have. Use while loops if you don't know how often the loop will run. Use for loops, if you know the number of runs in advance.

A typical for loop will combine your declaration (int i=0), the condition (i <= numbersProvide) and the missing increment (i++) in one line.

for (int i=0; i<=numbersProvide; i++)

When seeing that, I immediately recognize that the loop is off by one and it should be

for (int i=0; i<numbersProvide; i++)
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Hey Thomas, thanks for responding I will note everything you are saying. The for loop actually gave me what I wanted. I should have mentioned this in the post but I was also concerned with if the user decides to enter more then number after the loop ends it still says "Please enter a number" when it should say "Please enter number 2" and so forth? Any immediate advice? –  May 29 '22 at 17:11
  • I always like to get feedback on my progress, so yes, having "Enter number x of y" makes it a better usability. If the user shall be able to enter arbitrarily many numbers, then don't ask for the amount in advance and let him enter "E" for End or something. In that case you'd use a while loop `while (userInput != "E")` or something. – Thomas Weller May 30 '22 at 07:20