Jason Merrigan Jason Merrigan - 26 days ago 11
C# Question

Can anyone help me with this simple For Loop?

/* Write a program that asks the user
* to enter the starting point and end
* point of the counting range and the
* increment value and displays the total
* of the numbers within that range
*/

int start;
int end;
int increment;
int sum = 0;
int count= 0;

Console.WriteLine(" Enter the start number ");
start = Int32.Parse(Console.ReadLine());

Console.WriteLine(" Enter the end number ");
end = Int32.Parse(Console.ReadLine());

Console.WriteLine(" Enter the increment number ");
increment = Int32.Parse(Console.ReadLine());

for ( start = ; end <= start ; count = count + increment )
{

Console.WriteLine(" Number is: " + count);

}

Console.WriteLine(" Sum is: " + sum);
Console.ReadKey();

Answer

I modify your code a bit

/* Write a program that asks the user
 * to enter the starting point and end
 * point of the counting range and the 
 * increment value and displays the total 
 * of the numbers within that range
 */

int start;
int end;
int increment;
int sum = 0;
int count= 0;

Console.WriteLine(" Enter the start number ");
start = Int32.Parse(Console.ReadLine());

Console.WriteLine(" Enter the end number ");
end = Int32.Parse(Console.ReadLine());

Console.WriteLine(" Enter the increment number ");
increment = Int32.Parse(Console.ReadLine());

for ( count = start; //init value for count
      count <= end ; //check every loop. if count still satify condition, then do thing inside tho loop
      count += increment //change count every a loop done
    )
{
    sum += count; 
    Console.WriteLine(" Number is: " + count);

}

Console.WriteLine(" Sum is: " + sum);
Console.ReadKey();
Comments