Holow Holow - 3 months ago 7
C Question

What loop should I use and how?

The program is, the user will input the start value, end value and the interval. The output should be, the start_value will be added to the interval until it reach to the end value. I want to display some message if the output is greater than the end value. Example:

Enter start value:5
Enter end value: 30
Enter interval value: 5
Output: 5 10 15 20 25 30 // correct output
2nd try
Enter start value:5
Enter end value: 30
Enter interval value: 6
Output: 5 11 17 23 29 35 // wrong output


My code:

while(start_value <= end_value)
{
start_value = start_value + interval_value;
printf("%d ",start_value);
}

Answer

You need to print first, then increment.

while(start_value <= end_value)
{
    printf("%d ",start_value);                          
    start_value = start_value + interval_value;             
}