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();
}
}
}
You set
int i = 0;andinever increases. Nor doesnumbersProvidedecrease.Since you have a fixed amount of times that the loopo shall run, a
forloop is much more suitable for the situation than thewhileloop you have. Usewhileloops if you don't know how often the loop will run. Useforloops, 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.When seeing that, I immediately recognize that the loop is off by one and it should be